Flutter应用性能监控插件flutter_insights的使用
Flutter应用性能监控插件flutter_insights的使用
Periculum Insight插件(Flutter) 此插件允许Flutter应用程序生成移动数据分析和可负担性分析。
平台支持
- Android
安装和使用
- 在
pubspec.yaml
文件中添加插件的最新版本,并运行pub get
:
dependencies:
flutter_insights: ^0.0.2
- 导入插件并在Flutter应用中使用:
import 'package:flutter_insights/flutter_insights.dart';
必需权限
在调用包中的方法之前,需要在AndroidManifest.xml
中请求以下权限:
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
生成移动分析(SDK版本1)
参数表:
参数名 | 类型 | 描述 |
---|---|---|
publicKey |
String | 必填 |
phoneNumber |
String | 必填 |
bvn |
String | 必填 |
返回一个PericulumResponse
对象:
var flutterPericulum = await FlutterInsights.generateMobileAnalysis(
publicKey: 'publicKey',
bvn: '1234567890123',
phoneNumber: '090********',
);
返回类型为PericulumResponse
:
{
"insightKey":"mobileInsightKey", // 数字
"statusCode":"statusCode", // 数字
"status":"status", // 布尔值
"message":"message" // 字符串
}
生成移动洞察(SDK版本2)
参数表:
参数名 | 类型 | 描述 |
---|---|---|
publicKey |
String | 必填 |
phoneNumber |
String | 必填 |
bvn |
String | 必填 |
返回一个PericulumResponse
对象:
PericulumResponse flutterPericulum = await FlutterInsights.generateMobileInsightV2(
publicKey: 'publicKey',
bvn: '1234567890123',
phoneNumber: '090********',
);
更新现有的移动分析(SDK版本2)
参数表:
参数名 | 类型 | 描述 |
---|---|---|
publicKey |
String | 必填 |
phoneNumber |
String | 可选 |
bvn |
String | 可选 |
返回一个包含PericulumResponse
的Json对象:
PericulumResponse flutterPericulum = await FlutterInsights.patchMobileAnalysisV2(
publicKey: 'publicKey',
overviewkey: 'overviewKey',
bvn: '12345678908765',
phoneNumber: '090********',
);
示例代码:
ElevatedButton(
onPressed: () async {
try {
var flutterPericulum = await FlutterInsights.generateMobileInsightV2(
publicKey: 'nucleusis123',
bvn: '344983985053053',
phoneNumber: '09098983930',
);
setState(() {
responseOutput = flutterPericulum.toString();
});
} on Exception catch (e) {
throw e.toString();
}
},
child: const Text('Mobile Analysis V2'),
),
兼容性
- 最小Android SDK:
Periculum需要最低API级别为21。
信息
为了使该插件正常工作,您必须拥有一个Periculum账户,并且需要使用您的Client Id和secret key从Periculum API生成一个token。详情请参阅文档。
SDK用户流程
+-----------------------------------------------------+
| 用户流程图 |
+-----------------------------------------------------+
| |
v |
[1] 商户应用请求客户端设备权限 |
| |
| |
v |
[2] 商户应用调用后端服务器生成访问令牌 |
| |
| |
v |
[3] 数据从客户设备拉取,商户应用调用SDK处理数据 |
| |
| |
v |
[4] SDK调用Insights API生成移动洞察 |
| |
| |
v |
[End] 用户流程结束 |
| |
+-----------------------------------------------------+
开始使用
有关如何开始使用Flutter的信息,请参阅我们的在线文档,其中包含教程、示例、移动开发指南和完整的API参考。
完整示例Demo
import 'dart:developer';
import 'package:flutter/material.dart';
import 'package:flutter_insights/flutter_insights.dart';
void main() async {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool isLoading = false;
String responseOutput = "";
[@override](/user/override)
void initState() {
super.initState();
}
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('插件示例应用'),
),
body: isLoading
? const CircularProgressIndicator()
: SingleChildScrollView(
physics: const BouncingScrollPhysics(),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton(
onPressed: () async {
try {
var flutterPericulum = await FlutterInsights.generateMobileAnalysisV1(
publicKey: 'insert',
bvn: 'insert',
phoneNumber: 'insert',
);
setState(() {
responseOutput = flutterPericulum.toString();
});
} catch (e) {
rethrow;
}
},
child: const Text('移动分析V1'),
),
ElevatedButton(
onPressed: () async {
try {
var flutterPericulum = await FlutterInsights.generateMobileInsightV2(
publicKey: 'insert',
bvn: 'insert',
phoneNumber: 'insert',
);
setState(() {
responseOutput = flutterPericulum.toString();
});
} on Exception catch (_) {
rethrow;
}
},
child: const Text('移动分析V2'),
),
ElevatedButton(
onPressed: () async {
try {
var flutterPericulum = await FlutterInsights.patchMobileAnalysisV2(
publicKey: 'insert',
overviewkey: 'insert',
bvn: 'insert',
phoneNumber: 'insert',
);
setState(() {
responseOutput = flutterPericulum.toString();
});
} on Exception catch (e) {
log(e.toString());
// throw e.toString();
}
},
child: const Text('补丁分析V2'),
),
Text("结果: $responseOutput"),
],
),
),
),
);
}
}
更多关于Flutter应用性能监控插件flutter_insights的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter应用性能监控插件flutter_insights的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
flutter_insights
是一个用于监控 Flutter 应用性能的插件,它可以帮助开发者收集和分析应用运行时的性能数据,如帧率、内存使用、CPU 使用率等。通过使用 flutter_insights
,开发者可以更好地了解应用的性能瓶颈,并优化应用的性能。
安装 flutter_insights
首先,你需要在 pubspec.yaml
文件中添加 flutter_insights
依赖:
dependencies:
flutter_insights: ^0.1.0 # 使用最新版本
然后,运行 flutter pub get
来安装依赖。
基本使用
1. 初始化 flutter_insights
在你的 Flutter 应用启动时,初始化 flutter_insights
。通常可以在 main.dart
文件中进行初始化:
import 'package:flutter/material.dart';
import 'package:flutter_insights/flutter_insights.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 flutter_insights
await FlutterInsights.initialize(
apiKey: 'YOUR_API_KEY', // 替换为你的 API Key
enableDebugLogging: true, // 开启调试日志
);
runApp(MyApp());
}
2. 监控性能数据
flutter_insights
会自动开始监控应用的性能数据,如帧率、内存使用、CPU 使用率等。你可以在应用的任何地方手动触发性能监控:
FlutterInsights.startMonitoring();
3. 自定义事件
你可以使用 flutter_insights
来记录自定义事件,以便更好地分析应用的行为:
FlutterInsights.trackEvent('button_clicked', {'button_id': 'login_button'});
4. 查看性能数据
flutter_insights
会将收集到的性能数据发送到指定的服务器或平台,你可以在相应的平台上查看和分析这些数据。
高级配置
flutter_insights
提供了多种配置选项,允许你根据需求进行定制。例如,你可以配置采样率、设置自定义的日志级别等:
await FlutterInsights.initialize(
apiKey: 'YOUR_API_KEY',
enableDebugLogging: true,
samplingRate: 0.5, // 采样率为 50%
logLevel: LogLevel.verbose, // 设置日志级别
);
注意事项
- API Key: 确保你使用正确的 API Key,以便将数据发送到正确的平台。
- 数据隐私: 确保你了解并遵守数据隐私政策,特别是在处理用户数据时。
- 性能开销: 虽然
flutter_insights
设计为轻量级,但在生产环境中仍需注意其可能带来的性能开销。
示例
以下是一个完整的示例,展示了如何在 Flutter 应用中使用 flutter_insights
:
import 'package:flutter/material.dart';
import 'package:flutter_insights/flutter_insights.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// 初始化 flutter_insights
await FlutterInsights.initialize(
apiKey: 'YOUR_API_KEY',
enableDebugLogging: true,
);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Insights Demo',
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter Insights Demo'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
// 记录自定义事件
FlutterInsights.trackEvent('button_clicked', {'button_id': 'login_button'});
},
child: Text('Click Me'),
),
),
);
}
}