Flutter崩溃捕获与分析插件bugly_pro_flutter的使用
Flutter崩溃捕获与分析插件bugly_pro_flutter的使用
项目介绍
简介
bugly_pro_flutter
是腾讯端服务中台提供的 Flutter 线上性能监控组件,提供 Dart 异常捕获、启动性能、卡顿等监控能力。并上报数据到 Bugly 平台。
接入方式
- 在 Bugly 创建产品,接入平台选择 Flutter。
- 根据业务需要购买流量包,并绑定产品。
- 查询刚刚注册的产品信息: 进入刚刚注册的产品,打开 设置 --> 产品信息 ,记录下 APP ID 和 APP KEY ,后续有用。
- 在
pubspec.yaml
中添加依赖:
bugly_pro_flutter:
version: ^0.4.4
- 在项目的
main.dart
文件的main()
方法中初始化bugly_pro_flutter
:
import 'package:bugly_pro_flutter/bugly.dart';
void main() {
BuglyOptions options = BuglyOptions(
appId: '',
appKey: '',
bundleId: ''
);
options.monitorTypes = [
MonitorType.launchMetric,
MonitorType.looperMetric,
MonitorType.looperStack,
MonitorType.exception
];
options.userId = 'pro_tester'; // 用户账户信息(比如qq号,手机号等等),用于排查问题
Bugly.init(options, appRunner: () {
runApp(const MyApp());
}, beforeInitRunner: (options) {
options.appId = 'afxxxxxa01'; // 前面记录的APP ID
options.appKey = 'aef434ba-xxxx-xxxx-xxxx-xxxxxxxaae88b'; // 前面记录的APP KEY
options.bundleId = 'com.tencent.pro_test.hod'; // bundleId,根据实际情况填上即可
});
}
- 触发一个 Dart 错误,去 Bugly 的 错误 --> 问题列表 下查看错误信息,验证接入情况。
示例代码
import 'package:flutter/material.dart';
import 'package:tencent_flutter_apm_example/exception_page.dart';
import 'package:bugly_pro_flutter/bugly.dart';
import 'package:trouter/router/router_container.dart';
import 'package:trouter/t_router.dart';
import 'main_page.dart';
import 'jank/jank_main_page.dart';
import 'jank/jank_io_page.dart';
import 'jank/jank_listview_page.dart';
void main() {
BuglyOptions options = BuglyOptions(
appId: '',
appKey: '',
bundleId: '');
options.userId = '1234567';
options.isDebug = true;
options.onErrorCallback = (error) {
print("------------------------- bugly callback -------------------------");
print("$error");
print("------------------------- bugly callback -------------------------");
};
options.monitorTypes = [
MonitorType.launchMetric,
MonitorType.looperMetric,
MonitorType.looperStack,
MonitorType.exception
];
Bugly.enableLooperStack(true);
Bugly.init(options, appRunner: () {
runApp(MyApp());
}, beforeInitRunner: (options) {
options.userId = 'pro_tester';
options.appId = 'your appId';
options.appKey = 'your appKey';
options.bundleId = 'your app bundleId';
});
}
class MyApp extends StatefulWidget {
[@override](/user/override)
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
String _platformVersion = 'Unknown';
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: MyHome(),
navigatorObservers: [BuglyNavigatorObserver()],
routes: {
'MainPage': (context) => MainPage(),
'FirstPage': (context) => FirstPage(),
'SecondPage': (context) => SecondPage(),
'JankMainPage': (context) => JankMainPage(),
'JankListviewPage': (context) => JankListviewPage(),
'JankIoPage': (context) => JankIoPage(),
'ExceptionPage': (context) => ExceptionPage(),
},
);
}
}
class MyHome extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: Text('FPS 测试页面', style: TextStyle(fontSize: 16)),
onPressed: () => Navigator.of(context).pushNamed('MainPage'),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: Text('卡顿测试页面', style: TextStyle(fontSize: 16)),
onPressed: () => Navigator.of(context).pushNamed('JankMainPage'),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: Text('异常测试页面', style: TextStyle(fontSize: 16)),
onPressed: () => Navigator.of(context).pushNamed('ExceptionPage'),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextButton(
child: Text('开始上报', style: TextStyle(fontSize: 16)),
onPressed: () => Bugly.enableReport(),
),
],
)
],
),
);
}
}
更多关于Flutter崩溃捕获与分析插件bugly_pro_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter崩溃捕获与分析插件bugly_pro_flutter的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
bugly_pro_flutter
是一个用于捕获和分析 Flutter 应用崩溃的插件,它集成了腾讯的 Bugly 服务,能够帮助开发者快速定位和解决应用中的崩溃问题。以下是 bugly_pro_flutter
的使用指南,包括安装、配置和基本的错误捕获与分析。
1. 安装 bugly_pro_flutter
插件
首先,你需要在 pubspec.yaml
文件中添加 bugly_pro_flutter
依赖:
dependencies:
bugly_pro_flutter: ^1.0.0 # 请使用最新版本
然后运行 flutter pub get
来安装插件。
2. 配置 Bugly
在 Flutter 应用的 main.dart
文件中,初始化 Bugly
插件。通常,你需要在应用的入口处(如 main
函数)进行初始化。
import 'package:bugly_pro_flutter/bugly_pro_flutter.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 Bugly
await Bugly.init(
androidAppId: 'YOUR_ANDROID_APP_ID', // 替换为你的 Android App ID
iOSAppId: 'YOUR_IOS_APP_ID', // 替换为你的 iOS App ID
debugMode: true, // 调试模式,发布时设置为 false
);
runApp(MyApp());
}
3. 捕获未捕获的异常
bugly_pro_flutter
插件会自动捕获和管理未捕获的异常。你不需要额外编写代码来捕获这些异常。
4. 手动报告异常
如果你希望在代码中手动捕获并报告异常,可以使用 Bugly.postException
方法:
try {
// 可能会抛出异常的代码
} catch (e, stackTrace) {
// 手动报告异常
Bugly.postException(
error: e.toString(),
stackTrace: stackTrace.toString(),
);
}
5. 设置用户标识
为了更好地区分不同用户的崩溃日志,你可以设置用户标识:
Bugly.setUserId('USER_ID'); // 替换为你的用户标识
6. 设置标签
你可以为崩溃日志设置标签,以便于分类和筛选:
Bugly.setTag(1); // 1 是标签 ID,可以根据需要设置
7. 设置自定义信息
你还可以为崩溃日志添加自定义信息:
Bugly.setUserValue('key', 'value'); // 设置自定义键值对