flutter catcher插件的使用 全局异常捕获 并上传服务器

catcher插件的使用

void main() {
  CatcherOptions debugOptions =
      CatcherOptions(PageReportMode(showStackTrace: true), [ConsoleHandler()]);
  CatcherOptions releaseOptions =
      CatcherOptions(PageReportMode(showStackTrace: true), []);

  Catcher(MyApp(), debugConfig: debugOptions, releaseConfig: releaseOptions);
}
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    returnMaterialApp(
        navigatorKey: Catcher.navigatorKey,
        localizationsDelegates: [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
        ],
        supportedLocales: [
          const Locale('en', 'US'),
          const Locale('pl', 'PL'),
        ],
        theme: new ThemeData(
          primaryColor: Color.fromRGBO(236, 237, 236, 1),
        ),
        home: Scaffold(
          body: LoginPage(),
        ),
      );
  }
}
上传服务器

//强制竖屏
 SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
      .then((_) {
        CatcherOptions debugOptions = CatcherOptions(SilentReportMode(), [
          PostHandler(HttpRequestType.post, Uri.parse("${Url.memberMSGPublicUrl}"),
              printLogs: true)
        ]);
        CatcherOptions releaseOptions = CatcherOptions(SilentReportMode(), [
        //错误请求
          PostHandler(HttpRequestType.post, Uri.parse("$pathUrl),
              printLogs: true)
        ]);
        Catcher(MyApp(), debugConfig: debugOptions, releaseConfig: releaseOptions);
  });

用这些插件可能会遇到版本问题,按照错误的提示 基本上都可以解决的。
一般先确定自己使用的插件版本是不是最新的
其次就是android中  build。gradle里面的一些配置了

gradle.properties在这个文件中加上 
android.enableJetifier=true
android.useAndroidX=true
可以解决androidX那个版本错误。可以提前加上 。
做下记录方便以后使用。


//PostHandler 实现类
class PostHandler extends ReportHandler{
  final Dio _dio = Dio();
  final Logger _logger = Logger("HttpHandler");

  final HttpRequestType requestType;
  final Uri endpointUri;
  final Map<String, dynamic> headers;
  final int requestTimeout;
  final int responseTimeout;
  final bool printLogs;

  PostHandler(this.requestType, this.endpointUri,
      {this.headers = const {},
      this.requestTimeout = 5000,
      this.responseTimeout = 5000,
      this.printLogs = false}) {
    assert(this.requestType != null, "Request type can't be null");
    assert(this.endpointUri != null, "Endpoint uri can't be null");
  }

  @override
  Future<bool> handle(Report error) {
    // TODO: implement handle

    if (requestType == HttpRequestType.post) {
      return _sendPost(error);
    }
    return SynchronousFuture(true);
  }

  //发送请求上传错误信息
  Future<bool> _sendPost(Report error) async {
    //获取应用信息
      PackageInfo   packageInfo = await PackageInfo.fromPlatform();
      String pkg_name=packageInfo.packageName.isNotEmpty?packageInfo.packageName:"";
      String ver_name= packageInfo.version.isNotEmpty? packageInfo.version:"";
      String device_id= DataManager.instance.driversId.isNotEmpty? DataManager.instance.driversId:"";
      int uid= DataManager.instance.userinfo.uid;
      int ver_code= int.parse(packageInfo.buildNumber)!=null? int.parse(packageInfo.buildNumber):0;
    try {
      var data = {
      date:data
      };
      Options options =
          Options(sendTimeout: requestTimeout, receiveTimeout: responseTimeout, headers: headers);
      
      Response response = await _dio.post("url", data: data, options: options);
          print("HttpHandler response status: ${response.statusCode} body: ${response.data}");
      
      return SynchronousFuture(true);
    } catch (error, stackTrace) {
      return SynchronousFuture(false);
    }
  }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值