1. Catch the global exception of Flutter. The code is as follows:

Future<Null> _reportError(Dynamic Error, dynamic stackTrace) async { VIPFlutterCrashChannel.postException(error, stackTrace); } void main() {// register the exception callback of the Flutter frame. flutterError. onError = (FlutterErrorDetails Details) async {// Forward the error exception to the Zone error callback Zone.current.handleUncaughtError(details.exception, details.stack); }; RunZoned <Future<Null>>(() async {runApp(DDApp())); }, onError: (error, stackTrace) async { await _reportError(error, stackTrace); }); }Copy the code

Encapsulation of the Flutter and host platform communications

Class VIPFlutterCrashChannel {// Initialize channel static const MethodChannel _channel = const MethodChannel('vip_flutter_crash_channel'); _channel.invokeMethod("setUp",{'app_id':appID}); } static void postException(error, stack) {// report the exception and stack to Bugly _channel.invokemethod ("postException", {'crash_message':error.toString(),'crash_detail':stack.toString()}); }}Copy the code

3. Implementing Bugly reporting on iOS and Android:

Implementation on iOS:

- (void)configChannel { NSString *channelName = @"vip_flutter_crash_channel"; self.messageChannel = [FlutterMethodChannel methodChannelWithName:channelName binaryMessenger:[YYTextWeakProxy proxyWithTarget:self]]; / / flutter to monitor / / FlutterEventChannel * evenChannal = [FlutterEventChannel eventChannelWithName: channelName binaryMessenger:self]; // [evenChannal setStreamHandler:self]; @weakify(self); [_messageChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {@ strongify (self); the if ([@ "setUp" isEqualToString: call the method]) {/ / Bugly SDK nsstrings * appID initialization method = call.arguments[@"app_id"]; [Bugly startWithAppId:appID]; } else if ([@"postException" isEqualToString:call.method]) { NSString *message = call.arguments[@"crash_message"]; NSString *detail = Call. The arguments [@ "crash_detail"]; NSArray * stack = [detail componentsSeparatedByString: @ "\ n"); / / call Bugly report data interface [Bugly reportExceptionWithCategory:4 name:message reason:stack[0] callStack:stack extraInfo:@{} terminateApp:NO]; result(@0); } The else {/ / method did not achieve the result (FlutterMethodNotImplemented);}}]; }Copy the code