Flutter: Use custom ErrorWidget

前端 未结 2 1028
-上瘾入骨i
-上瘾入骨i 2021-01-21 07:17

Is there a way to display (in production) a custom error widget instead of the red screen of death? I will not change the framework code, plus even though we are trying to progr

相关标签:
2条回答
  • 2021-01-21 07:31

    Use ErrorWidget.builder instead main() function, like this

    Future main() async {
      WidgetsFlutterBinding.ensureInitialized();
      ErrorWidget.builder = (FlutterErrorDetails details) => SomethingWrong();
      runApp(YourApp());
    }
    

    And if you want to show Certain Widget only on Production app, then you can use kReleaseMode-

      if (kReleaseMode) {
        ErrorWidget.builder = (FlutterErrorDetails details) => SomethingWrong();
      }
    

    Note: The top-level kReleaseMode constant is use to determine whether the app was compiled in release mode.

    0 讨论(0)
  • 2021-01-21 07:52

    It is possible to change the default error rendering by changing ErrorWidget.builder

    ErrorWidget.builder = (errorDetails) {
      return Container(color: Colors.red);
    }
    
    0 讨论(0)
提交回复
热议问题