本节目标

  • 使用 sentry 平台
  • flutter 集成
  • android 集成
  • ios 集成

视频

代码

https://github.com/ducafecat/flutter_learn_news/releases/tag/v1.0.12

正文

错误收集策略

sentry 平台

https://sentry.io

收集 flutter

  • 参考

https://docs.sentry.io/platforms/flutter/

  • pubspec.yaml
1
2
dependencies:
sentry: ^3.0.1
  • lib/main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 创建 SentryClient 用于将异常日志上报给 sentry 平台
final SentryClient _sentry = new SentryClient(
dsn:
'https://xxxxxxxxxx',
);

// 是否开发环境
bool get isInDebugMode {
return false; // false 开始上传 sentry
}

// 上报异常的函数
Future<void> _reportError(dynamic error, dynamic stackTrace) async {
print('Caught error: $error');
if (isInDebugMode) {
print(stackTrace);
} else {
final SentryResponse response = await _sentry.captureException(
exception: error,
stackTrace: stackTrace,
);

if (response.isSuccessful) {
print('Success! Event ID: ${response.eventId}');
} else {
print('Failed to report to Sentry.io: ${response.error}');
}
}
}

Future<Null> main() async {
// 捕获并上报 Flutter 异常
FlutterError.onError = (FlutterErrorDetails details) async {
if (isInDebugMode == true) {
FlutterError.dumpErrorToConsole(details);
} else {
Zone.current.handleUncaughtError(details.exception, details.stack);
}
};

// 捕获并上报 Dart 异常
runZonedGuarded(() async {
await Global.init();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<AppState>.value(
value: Global.appState,
),
],
child: Consumer<AppState>(builder: (context, appState, _) {
if (appState.isGrayFilter) {
return ColorFiltered(
colorFilter: ColorFilter.mode(Colors.white, BlendMode.color),
child: NewsApp(),
);
} else {
return NewsApp();
}
}),
),
);
}, (Object error, StackTrace stack) {
_reportError(error, stack);
});
}

收集 android

  • 参考

https://docs.sentry.io/platforms/android/

  • 集成 sdk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// ADD JCENTER REPOSITORY
repositories {
jcenter()
}

// ADD COMPATIBILITY OPTIONS TO BE COMPATIBLE WITH JAVA 1.8
android {
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}

// ADD SENTRY ANDROID AS A DEPENDENCY
dependencies {
// https://github.com/getsentry/sentry-android/releases
implementation 'io.sentry:sentry-android:{version}'
}
  • android/app/src/main/AndroidManifest.xml
1
2
3
4
5
6
7
8
9
10
<application
android:name="io.flutter.app.FlutterApplication"
android:label="猫哥新闻"
android:icon="@mipmap/launcher_icon">

...

<!-- sentry -->
<meta-data android:name="io.sentry.dsn" android:value="xxxxxxxxxxxxxxxxx" />
</application>
  • android/app/src/main/kotlin/com/example/flutterducafecatnews/CrashHandler.java
1
2
3
4
5
6
7
public class CrashHandler implements UncaughtExceptionHandler {

@Override
public void uncaughtException(Thread t, Throwable e) {
Sentry.captureException(e);
}
}
  • android/app/src/main/kotlin/com/example/flutterducafecatnews/MainActivity.kt
1
2
3
4
5
6
7
8
9
import io.sentry.core.Sentry

class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
val crashHandler = CrashHandler()
Thread.setDefaultUncaughtExceptionHandler(crashHandler)
GeneratedPluginRegistrant.registerWith(flutterEngine)
}
}

收集 ios

  • 资料

https://docs.sentry.io/platforms/cocoa/?_ga=2.17974013.534595501.1591172359-228174411.1591172359&_gac=1.12380800.1591172359.EAIaIQobChMIrICd9Jrl6QIVCj5gCh2zFw8lEAAYASAAEgJwyfD_BwE&platform=javascript

  • 集成 CocoaPods
1
2
3
4
5
6
platform :ios, '8.0'
use_frameworks! # This is important

target 'YourApp' do
pod 'Sentry', :git => 'https://github.com/getsentry/sentry-cocoa.git', :tag => '5.1.2'
end
  • ios/Runner/AppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{

SentrySDK.start(options: [
"dsn": "https://xxxxxxxxxxxxxxxxxxx",
"debug": true, // Enabled debug when first installing is always helpful
"enableAutoSessionTracking": true
])

NSSetUncaughtExceptionHandler { exception in
print(exception)
SentrySDK.capture(message: exception.description)
SentrySDK.capture(exception: exception)
}

GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)

}

资源

参考

设计稿蓝湖预览

https://lanhuapp.com/url/lYuz1
密码: gSKl

蓝湖现在收费了,所以查看标记还请自己上传 xd 设计稿
商业设计稿文件不好直接分享, 可以加微信联系 ducafecat


© 猫哥

https://ducafecat.tech