【问题标题】:Flutter: _route == ModalRoute.of(context)': is not true on dialogFlutter:_route == ModalRoute.of(context)':在对话框中不正确
【发布时间】:2021-07-08 18:53:49
【问题描述】:
final _sectionFormKey = GlobalKey<FormState>();

...
TextButton.icon(
              onPressed: _addSection,
              icon: Icon(Icons.info_outline_rounded),
              label: Text("open"),
            ),
...

_addSection() {
    return showDialog<void>(
      context: context,
      builder: (context) => PlatformAlertDialog(
        title: Text(translate("other_details.extra_sections")),
        content: Form(
          key: _sectionFormKey,
          child: Text(translate("other_details.extra_sections_info")),
        ),
        actions: <Widget>[
          TextButton(
            child: new PlatformText((translate('ok'))),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      ),
    );
  }

如果我在按钮上单击太快(一个显示对话框,然后再次单击背景关闭它)我会收到以下错误:

'package:flutter/src/widgets/will_pop_scope.dart':断言失败: 第 135 行 pos 12: '_route == ModalRoute.of(context)': is not true.

'package:flutter/src/widgets/framework.dart':断言失败:行 4954 pos 14: '_dependents.isEmpty': 不正确。

要么断言表明框架本身存在错误,要么我们 应在此错误消息中提供更多信息 帮助您确定并解决根本原因。

【问题讨论】:

  • 如果你使用Navigator.pop(context);会发生什么?

标签: flutter flutter-layout


【解决方案1】:
Please change your code to:
final _sectionFormKey = GlobalKey<FormState>();

...
TextButton.icon(
              onPressed: _addSection,
              icon: Icon(Icons.info_outline_rounded),
              label: Text("open"),
            ),
...

_addSection() {
    return showDialog<void>(
      context: context,
      builder: (context) => PlatformAlertDialog(
        title: Text(translate("other_details.extra_sections")),
        content: Form(
          key: _sectionFormKey,
          child: Text(translate("other_details.extra_sections_info")),
        ),
        actions: <Widget>[
          TextButton(
            child: new PlatformText((translate('ok'))),
            onPressed: () {
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );
  }

【讨论】:

  • onPressed: Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) => _addSection()));
猜你喜欢
  • 2021-02-23
  • 1970-01-01
  • 2021-08-07
  • 2021-07-24
  • 1970-01-01
  • 2021-06-08
  • 2020-01-06
  • 2020-03-13
  • 2014-07-14
相关资源
最近更新 更多