显示用户友好的错误页面,而不是Flutter中的异常 [英] Show user-friendly error page instead of exception in Flutter
问题描述
是否可以进行全局错误处理,以显示用户友好的错误页面,而不显示红色异常?
我已经进行了错误处理(
Is it possible to make global error handling that will show user-friendly error page instead of showing red exception?
I already made error handling (here) that will report exception to the backend but what I really would like to achieve is to hide red exception and show something a little bit frendlier.
I searched everywhere and I found literally nothing. I'm sorry that I don't have any code to show because I have no idea how to start.
There is a little static error builder hidden in the documentation.
When an error occurs while building a widget, the broken widget is replaced by the widget returned by this function. By default, an ErrorWidget is returned.
You can define in the the builder
method of the MaterialApp widget.
Widget buildError(BuildContext context, FlutterErrorDetails error) {
return Scaffold(
body: Center(
child: Text(
"Error appeared.",
style: Theme.of(context).textTheme.title,
),
),
);
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
builder: (BuildContext context, Widget widget) {
ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
return buildError(context, errorDetails);
};
return widget;
},
title: 'Flutter Demo',
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
这篇关于显示用户友好的错误页面,而不是Flutter中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!