首页 > 解决方案 > 禁用 Flutter 的“死机红屏”

问题描述

有没有办法禁用 Flutter 的“红屏死机”?我在调试期间不介意它,但它似乎也出现在生产版本中 - 我找不到任何关于如何禁用它的信息,或者我自己捕获错误。

我尝试用FlutterError.onErrorand捕捉杂散错误runZoned(onError),但两者都没有阻止红屏的出现。

作为参考,我说的是这个屏幕:

rsod

标签: error-handlingdartflutter

解决方案


您可以覆盖 ErrorWidget.builder 方法。
我解决了这个问题。

・示例代码。

void main() {
    ErrorWidget.builder = (FlutterErrorDetails details) => Container();
    ...
}

・默认代码

static ErrorWidgetBuilder builder = _defaultErrorWidgetBuilder;

我希望它的帮助。


2019 年 12 月 21 日更新

或更改 ErrorWidget backgroundColor 和 textStyle。

・示例代码

import 'dart:ui' as ui;
void main() {
    RenderErrorBox.backgroundColor = Colors.transparent;
    RenderErrorBox.textStyle = ui.TextStyle(color: Colors.transparent);
}

推荐阅读