微信交流群

老孟公众号

# ErrorWidget

屏蔽错误页面组件,ErrorWidget的构造函数的参数是exception的对象,然后返回一个内容是exception message信息的RenderBox.

正常错误页面

要想Flutter的错误页面显示成自定义的页面,只要设置ErrorWidgetbuilder就行

ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails){
    print(flutterErrorDetails.toString());
    return Center(
        child: Text("Flutter 走神了"),
    );
};
1
2
3
4
5
6

ErrorWidget.builder 返回一个Widget,当Flutter出错的时候就会显示这个Widget。

# ErrorWidget用法

ErrorWidget组件在build Widget的过程中调用。

案例

Widget built;
try {
    built = build();
} catch (e, stack) {
    //ErrorWidget.builder
    built = ErrorWidget.builder(_debugReportException('building $this', e, stack));
} 

try {
    built = build();
} catch (exception, stack) {
    //FlutterErrorDetails
    final FlutterErrorDetails details = FlutterErrorDetails(
        exception: exception,
        stack: stack,
        library: 'widgets library',
        context: 'attaching to the render tree'
    );
    FlutterError.reportError(details);
    final Widget error = ErrorWidget.builder(details);
    built = updateChild(null, error, _rootChildSlot);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

本文由Rock提供。