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);
}
}
}