美文网首页flutter
flutter 错误页面友好提示统一处理

flutter 错误页面友好提示统一处理

作者: 小码农CC | 来源:发表于2019-11-23 11:11 被阅读0次

    在MyApp 类中自定义错误页面

     Widget getErrorWidget(BuildContext context, FlutterErrorDetails error) {
        return Center(
          child: Text(
            "Error appeared.",
            style: Theme.of(context).textTheme.title.copyWith(color: Colors.red),
          ),
        );
      }
    

    然后在build(BuildContext context)中添加

     ErrorWidget.builder = (FlutterErrorDetails errorDetails) {
          return getErrorWidget(context, errorDetails);
        };
    

    //事例

    class MyApp extends StatelessWidget {
      
       Widget getErrorWidget(BuildContext context, FlutterErrorDetails error) { return Center( child: Text( "Error appeared.", style: Theme.of(context).textTheme.title.copyWith(color: Colors.red), ), ); }  
    
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
       ErrorWidget.builder = (FlutterErrorDetails errorDetails) { return getErrorWidget(context, errorDetails); };
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            // This is the theme of your application.
            //
            // Try running your application with "flutter run". You'll see the
            // application has a blue toolbar. Then, without quitting the app, try
            // changing the primarySwatch below to Colors.green and then invoke
            // "hot reload" (press "r" in the console where you ran "flutter run",
            // or simply save your changes to "hot reload" in a Flutter IDE).
            // Notice that the counter didn't reset back to zero; the application
            // is not restarted.
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    

    相关文章

      网友评论

        本文标题:flutter 错误页面友好提示统一处理

        本文链接:https://www.haomeiwen.com/subject/nexhwctx.html