天天看點

flutter 崩潰收集一、flutter 崩潰收集的方式二、Flutter異常收集最佳實踐三、Flutter crash 收集平台

目錄

一、flutter 崩潰收集的方式

1、通用方式

2、捕捉async異常

1)try/catch

2)使用 Future API 

         3)async異常 與 Future 的更多資訊

3、使用 runZoned

4、使用 FlutterError.onError

5、使用 Isolate.current.addErrorListener

二、Flutter異常收集最佳實踐

三、Flutter crash 收集平台

1、Sentry

1)商業Sentry伺服器

2)flutter官方支援

3)總結

2、Crashlytics (--> fabric --> Firebase)

1)flutter 官方支援的計劃

2)非官方flutter插件

3)總結

一、flutter 崩潰收集的方式

1、通用方式

use a try/catch block

2、捕捉async異常

1)try/catch

Future main() async {
  var dir = new Directory('/tmp');

  try {
    var dirList = dir.list();
    await for (FileSystemEntity f in dirList) {
      if (f is File) {
        print('Found file ${f.path}');
      } else if (f is Directory) {
        print('Found dir ${f.path}');
      }
    }
  } catch (e) {
    print(e.toString());
  }
}
           

2)使用 Future API 

myFunc()
  .then((value) {
    doSomethingWith(value);
    ...
    throw("some arbitrary error");
  })
  .catchError(handleError);
           

3)async異常 與 Future 的更多資訊

  • Futures and Error Handling
  • Asynchronous Programming: Futures
  • dart:async library

3、使用 runZoned

// This creates a [Zone] that contains the Flutter application and stablishes
// an error handler that captures errors and reports them.
//
// Using a zone makes sure that as many errors as possible are captured,
// including those thrown from [Timer]s, microtasks, I/O, and those forwarded
// from the `FlutterError` handler.
//
// More about zones:
//
// - https://api.dartlang.org/stable/1.24.2/dart-async/Zone-class.html
// - https://www.dartlang.org/articles/libraries/zones
runZoned<Future<Null>>(() async {
  runApp(new CrashyApp());
}, onError: (error, stackTrace) async {
  await _reportError(error, stackTrace);
});
           

4、使用 FlutterError.onError

FlutterError.onError is only about reporting flutter framework errors (errors that are caught by the framework typically during a frame).

// This captures errors reported by the Flutter framework.
FlutterError.onError = (FlutterErrorDetails details) async {
  if (isInDebugMode) {
    // In development mode simply print to console.
    FlutterError.dumpErrorToConsole(details);
  } else {
    // In production mode report to the application zone
    // 重定向到 3中的 runZone 中處理
    Zone.current.handleUncaughtError(details.exception, details.stack);
  }
};
           

5、使用 Isolate.current.addErrorListener

use Isolate.current.addErrorListener to capture uncaught errors in the root zone.

Isolate.current.addErrorListener(new RawReceivePort((dynamic pair) async {
  print('Isolate.current.addErrorListener caught an error');
  await _reportError(
    (pair as List<String>).first,
    (pair as List<String>).last,
  );
}).sendPort);
           

二、Flutter異常收集最佳實踐

Future<Null> main() async {
  FlutterError.onError = (FlutterErrorDetails details) async {
    if (isInDebugMode) {
      FlutterError.dumpErrorToConsole(details);
    } else {
      Zone.current.handleUncaughtError(details.exception, details.stack);
    }
  };

  runZoned<Future<Null>>(() async {
    runApp(new HomeApp());
  }, onError: (error, stackTrace) async {
    await _reportError(error, stackTrace);
  });
}
           

三、Flutter crash 收集平台

1、Sentry

1)商業Sentry伺服器

https://sentry.io

sentry.io 的服務是收費的,使用天數隻有13天左右,過期後不付費的話:隻儲存1w個事件,沒有成員功能。

flutter 崩潰收集一、flutter 崩潰收集的方式二、Flutter異常收集最佳實踐三、Flutter crash 收集平台

2)flutter官方支援

Sentry flutter package:https://pub.dartlang.org/packages/sentry

github: flutter/sentry

demo: flutter/crashy

3)總結

優點:有點因為有了 flutter package ,flutter 接入 Sentry,非常簡單。

缺點:免費版限制多。

2、Crashlytics (--> fabric --> Firebase)

1)flutter 官方支援的計劃

Please add support for Crashlytics

2)非官方flutter插件

https://github.com/kiwi-bop/flutter_crashlytics 

https://pub.dartlang.org/packages/flutter_crashlytics

3)總結

優點:Google旗下,免費!

缺點:目前flutter官方尚未有對應package/plugin,使用 flutter_crashlytics  對接比較麻煩。

注意:2019年中,fabric 服務将關閉,一律遷移至 Firebase。這個動作與微軟的 hockeyapp 很像。

Fabric将并入Firebase行動程式開發平台,明年中走入曆史

Crashlytics vs Fabric vs Firebase Crash Reporting — I'm lost