而不是Flutter中的异常

而不是Flutter中的异常

本文介绍了显示用户友好的错误页面,而不是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.

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中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 17:29