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;
}
Flutter 捕获异常
最新推荐文章于 2025-05-28 23:39:02 发布