Flutter 捕获异常

这段代码展示了在Flutter中如何使用Bugly进行异常捕获,并结合自定义函数reportError来记录错误信息。报告错误时,会将错误详情写入到应用的文件系统中,便于后续分析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FlutterBugly.postCatchedException((){
  false;
  runZonedGuarded(() { runApp(MyApp()); }, reportError);
});

​​​​​​​void reportError(Object, StackTrace) async {
  // print('收集到错误: $Object, $StackTrace');
  var path = await getFilePath('error');
  var name = utils.CustomStampToTime_str(Date: '${userInfo.userid}@YY.MM.DD');
  var _has = await checkCache('$name', file: 'error');
  File _errFile = File('${path}/${name}');
  String errMsg = await readCache(name, type: 'error');
  errMsg += '\n==================';
  errMsg += '\n';
  errMsg += '时间:${utils.CustomStampToTime_str(Date: 'YY-MM-DD hh:mm:ss')}';
  errMsg += '\n';
  errMsg += '$Object';
  errMsg += '\n';
  errMsg += '$StackTrace';
  errMsg += '\n';
  await _errFile.writeAsString(errMsg);
  // print('完成');
}


// 获取app临时文件路径
Future getFilePath(type) async{
    var filepath = await getApplicationDocumentsDirectory(); // App内部file:data.com.nufang.zao/app-flutter/
    // var filepath = await getTemporaryDirectory();        // App内部file:data.com.nufang.zao/ache/
    // var filepath = await getExternalStorageDirectory();  // App内部file:data.com.nufang.zao/file/

    var file;
    if(type == 'all'){
      file = Directory(filepath.path);
    }else{
      file = Directory(filepath.path + '/$type');
    }

    try {
      bool exists = await file.exists();
      if (!exists) {
        await file.create();
      }
    } catch (e) {
      print(e);
    }
    return file.path;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值