flutter 崩溃收集

目录

 

一、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>(() 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).first,
    (pair as List).last,
  );
}).sendPort);

 

 

二、Flutter异常收集最佳实践

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

  runZoned>(() 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个事件,没有成员功能。

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

 

 

 

 

 

你可能感兴趣的:(Android,iOS,Flutter,Crash)