flutter

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

2019-11-23  本文已影响0人  小码农CC

在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'),
    );
  }
}
上一篇 下一篇

猜你喜欢

热点阅读