【问题标题】:Getting "'context != null': is not true" error when trying to showDialogue after a MaterialPageRoute in Flutter?在 Flutter 中的 MaterialPageRoute 之后尝试 showDialogue 时出现“'context != null': is not true”错误?
【发布时间】:2020-08-01 11:10:25
【问题描述】:

我目前能够从 Flutter 的 Home 应用程序中将 MaterialRoute 路由到一个页面,并显示一个弹出对话框。但是,在从第二页路由到包含应该显示对话框的按钮的第三页时,我收到此错误:[VERBOSE-2:ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter/src/widgets/localizations.dart': Failed assertion: line 446 pos 12: 'context != null': is not true.

触发该错误的 showDialogue 如下所示:


class ThirdPageWidgetState extends State<ThirdPageWidget> {

  StreamSubscription<ScanResult> scanSubscription;

  @override
  void initState() {
    super.initState();
  }
Future<void> alert(deviceName) async {
    return showDialog<void>(
      barrierDismissible: false, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('Button Pressed!'),
          content: SingleChildScrollView(
            child: ListBody(
              children: <Widget>[
                Text('test'),
              ],
            ),
          ),
          actions: <Widget>[
            FlatButton(
              child: Text('Ok'),
            ),
          ],
        );
      },
    );
  }

  'Build function omitted'
}

第二页到第三页的路由是这样的:

void routeAppToThirdPage() async {
    Navigator.push(
      context,
      MaterialPageRoute(
        builder: (context) => ThirdPageWidget(),
      ),
    );
  }

【问题讨论】:

    标签: flutter dart mobile dart-pub


    【解决方案1】:

    标准 Flutter 对话框需要 BuildContext。您可以通过参数发送,您将不会再有错误。但是,如果您想节省开发时间或不喜欢通过参数发送上下文的想法,您可以简单地使用这个库,它允许您从代码中的任何位置导航路线、打开对话框和快餐栏,而不需要上下文。

    https://pub.dev/packages/get

    在您的示例中,您的代码也会更精简:

    导航到下一条路线:

    Get.to(ThirdPageWidget());
    

    打开对话框:

       Get.dialog(
        AlertDialog(
            title: Text('Button Pressed!'),
            content: SingleChildScrollView(
                child: ListBody(
              children: <Widget>[
                Text('test'),
              ],
            ))),
        barrierDismissible: false,
      );
    

    【讨论】:

      【解决方案2】:

      showDialogue&lt;void&gt;() 需要 context:context 参数,编译器没有捕获到该参数。

      return showDialog<void>(
            context: context // THIS WAS MISSING
            barrierDismissible: false, // user must tap button!
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('Button Pressed!'),
                content: SingleChildScrollView(
                  child: ListBody(
                    children: <Widget>[
                      Text('test'),
                    ],
                  ),
                ),
                actions: <Widget>[
                  FlatButton(
                    child: Text('Ok'),
                  ),
                ],
              );
          ```
      

      【讨论】:

        【解决方案3】:

        您收到此错误的原因是您没有将BuildContext 传递给showDialog,您必须进行更改。

         Future<void> alert(deviceName, context) async {...}
        //Then when you call your function in your build function you would pass in context
        await alert(deviceName, context);
        

        【讨论】:

        • 谢谢,很惊讶它正在编译
        • 不,这是一个不同的问题。 showDialogue() 在其中需要一个 context: context。全局上下文使用没问题。
        • 在我的情况下出现错误 - 断言失败:第 446 行 pos 12: 'context != null': is not true.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-30
        • 2022-11-19
        • 2021-02-25
        • 2021-05-12
        • 1970-01-01
        • 2021-01-15
        • 1970-01-01
        相关资源
        最近更新 更多