Flutter崩溃报告插件innim_agconnect_crash的使用
Flutter崩溃报告插件innim_agconnect_crash的使用
简介
innim_agconnect_crash 是一个支持 Null 安全的插件,它是对 agconnect_crash 的分支版本。该插件提供了强大的崩溃报告功能,可以快速检测、定位并解决应用程序的崩溃问题,并实时生成可读性高的崩溃报告。
引入
华为 AppGallery Connect 提供了一个轻量级的解决方案来处理应用崩溃问题。通过此服务,您可以轻松地在不编写任何代码的情况下检测到崩溃并查看详细的崩溃报告。
安装插件
要将 innim_agconnect_crash 插件添加到您的 Flutter 项目中,请按照以下步骤操作:
- 在项目的
pubspec.yaml文件中添加依赖:dependencies: innim_agconnect_crash: ^1.7.0 - 在终端运行以下命令以安装依赖:
flutter pub get
开发指南
使用方法
更多关于如何使用该插件的信息,请参考 AppGallery Connect Crash 使用指南。
参考文档
有关插件的详细 API 参考,请访问 AppGallery Connect 参考文档。
许可证
innim_agconnect_crash 插件遵循以下许可证:
示例代码
以下是一个完整的示例代码,展示了如何使用 innim_agconnect_crash 插件来记录崩溃信息。
/*
* Copyright 2020. Huawei Technologies Co., Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:innim_agconnect_crash/innim_agconnect_crash.dart';
void main() {
// 将 Flutter 错误捕获器设置为 AGCCrash 实例
FlutterError.onError = AGCCrash.instance.onFlutterError;
// 使用 runZonedGuarded 来捕获未处理的异常
runZonedGuarded<Future<void>>(
() async {
runApp(MyApp());
},
(Object error, StackTrace stackTrace) {
// 记录未处理的错误
AGCCrash.instance.recordError(error, stackTrace);
},
);
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
// 启用崩溃收集
_enableCollection() async {
await AGCCrash.instance.enableCrashCollection(true);
}
// 禁用崩溃收集
_disableCollection() async {
await AGCCrash.instance.enableCrashCollection(false);
}
// 测试非致命崩溃
_testCrash() async {
try {
throw Exception('test exception');
} catch (e, stack) {
AGCCrash.instance.recordError(e, stack);
}
}
// 测试致命崩溃
_testFatalCrash() async {
try {
throw Exception('test exception fatal');
} catch (e, stack) {
AGCCrash.instance.recordError(e, stack, fatal: true);
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
MaterialButton(
onPressed: _enableCollection,
color: Colors.blue,
child: Text(
'启用崩溃收集',
style: TextStyle(color: Colors.white),
)),
MaterialButton(
onPressed: _disableCollection,
color: Colors.blue,
child: Text(
'禁用崩溃收集',
style: TextStyle(color: Colors.white),
)),
MaterialButton(
onPressed: _testCrash,
color: Colors.blue,
child: Text(
'测试崩溃',
style: TextStyle(color: Colors.white),
)),
MaterialButton(
onPressed: _testFatalCrash,
color: Colors.blue,
child: Text(
'测试致命崩溃',
style: TextStyle(color: Colors.white),
)
),
],
)),
),
);
}
}
更多关于Flutter崩溃报告插件innim_agconnect_crash的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter崩溃报告插件innim_agconnect_crash的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
innim_agconnect_crash 是一个用于 Flutter 的崩溃报告插件,它集成了华为的 AGC (AppGallery Connect) Crash 服务,可以帮助开发者收集和分析应用崩溃信息。以下是使用 innim_agconnect_crash 插件的基本步骤:
1. 添加依赖
首先,在 pubspec.yaml 文件中添加 innim_agconnect_crash 插件的依赖:
dependencies:
flutter:
sdk: flutter
innim_agconnect_crash: ^latest_version
然后运行 flutter pub get 来安装依赖。
2. 初始化插件
在你的 Flutter 应用中初始化 innim_agconnect_crash 插件。通常,你可以在 main.dart 文件中进行初始化:
import 'package:flutter/material.dart';
import 'package:innim_agconnect_crash/innim_agconnect_crash.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化崩溃报告插件
await InnimAgconnectCrash.instance.init();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
3. 配置 AGC 控制台
在华为 AGC 控制台中,确保你已经启用了 Crash 服务,并且下载了 agconnect-services.json 文件。将该文件放置在 Flutter 项目的 android/app 目录下。
4. 测试崩溃报告
为了确保崩溃报告功能正常工作,你可以在代码中手动触发一个崩溃,然后查看 AGC 控制台是否能够捕获到崩溃信息。
void triggerCrash() {
List<int> list = [];
print(list[1]); // 这将导致数组越界崩溃
}
你可以在某个按钮的点击事件中调用 triggerCrash 函数来触发崩溃。
5. 查看崩溃报告
在应用崩溃后,打开华为 AGC 控制台,进入 Crash 服务页面,你应该能够看到刚刚发生的崩溃信息,包括堆栈跟踪、设备信息等。
6. 自定义崩溃处理
你可以通过 InnimAgconnectCrash 提供的 API 来设置自定义的崩溃处理器或添加额外的信息:
InnimAgconnectCrash.instance.setCustomKey('user_id', '12345');
InnimAgconnectCrash.instance.log('Custom log message');
7. 处理未捕获的异常
你还可以使用 FlutterError.onError 来捕获 Flutter 框架中的未捕获异常,并将其报告给 AGC Crash 服务:
FlutterError.onError = (FlutterErrorDetails details) async {
// 将异常信息报告给 AGC Crash 服务
await InnimAgconnectCrash.instance.recordFlutterError(details);
};
8. 处理 Dart 异常
如果你想捕获 Dart 代码中的未捕获异常,可以使用 runZonedGuarded:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化崩溃报告插件
await InnimAgconnectCrash.instance.init();
runZonedGuarded(() {
runApp(MyApp());
}, (error, stackTrace) async {
// 将 Dart 异常报告给 AGC Crash 服务
await InnimAgconnectCrash.instance.recordError(error, stackTrace);
});
}

