颤动:使用navigator.dart时失败断言

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了颤动:使用navigator.dart时失败断言相关的知识,希望对你有一定的参考价值。

我是Flutter的新手并且玩弄它。所以,请耐心等待我。

单击PopupMenuButton的特定菜单项时会抛出以下异常,但始终只是第二次:

'package:flutter / src / widgets / navigator.dart':断言失败:第1846行第12行:'!_debugLocked':不是真的。

这里设置:

为了指定菜单项,已经定义了以下类:

class PopupMenuChoice {
  const PopupMenuChoice({this.title, this.pageRoute});

  final String title;
  final MaterialPageRoute pageRoute;
}

在AppBar的actions属性中定义PopupMenuButton:

new PopupMenuButton<PopupMenuChoice>(
    itemBuilder: (BuildContext context) {
      return _popupMenus.map((PopupMenuChoice choice) {
        return new PopupMenuItem<PopupMenuChoice>(
          value: choice,
          child: new Text(choice.title),
        );
      }).toList();
    },
    onSelected: _popupMenuSelected,
),

相应的小部件在下面的类中定义(AppBar在此类的“return new Scaffold”中创建):

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];
  final _saved = new Set<WordPair>();
  final _popupMenus = <PopupMenuChoice>[];

  ...
}

正如您所看到的,有用于保存WordPair对象的私有变量,还有用于菜单选项的私有变量。

_popupMenus列表在“构建覆盖”中设置:

@override
Widget build(BuildContext context) {
    // Setup page routes
    if (_popupMenus.where((p) => p.title == 'Saved Suggestions').length == 0) {
      final _pageRouteSavedSuggestions = new MaterialPageRoute(
        builder: (context) {
          final tiles = _saved.map(
            (pair) {
              return new ListTile(
                title: new Text(
                  pair.asPascalCase,
                  style: _biggerFont,
                ),
              );
            },
          );
          final divided = ListTile
              .divideTiles(
                context: context,
                tiles: tiles,
              )
              .toList();

          return new Scaffold(
            appBar: new AppBar(
              title: new Text('Saved Suggestions'),
            ),
            body: new ListView(children: divided),
          );
        },
      );
      _popupMenus.add(new PopupMenuChoice(
          title: 'Saved Suggestions', pageRoute: _pageRouteSavedSuggestions));
    }

    if (_popupMenus.where((p) => p.title == 'TEST Page').length == 0) {
      final _pageRouteTest = new MaterialPageRoute(
        builder: (context) {
          return new Scaffold(
            appBar: new AppBar(
              title: new Text('TEST Page'),
            ),
            body: new Text('Some content...'),
          );
        },
      );
      _popupMenus.add(
          new PopupMenuChoice(title: 'TEST Page', pageRoute: _pageRouteTest));
    }
    ...

在PopupMenuChoice定义的MaterialPageRoute中,私有变量可能是访问权限(例如_saved)。

这里是onSelected的PopupMenuButton的相应事件处理程序:

void _popupMenuSelected(PopupMenuChoice choice) {
  Navigator.of(context).push(choice.pageRoute);
}

任何人都可以解释为什么抛出这个异常?它怎么能被阻止?

谢谢,罗杰


在特定菜单项上单击第二次时,调试控制台中的其他信息:

E / flutter(17133):[错误:topaz / lib / tonic / logging / dart_error.cc(16)]未处理的异常:E / flutter(17133):'package:flutter / src / widgets / routes.dart':失败断言:第177行pos 12:'!_ transnsitionCompleter.isCompleted':处理后无法安装MaterialPageRoute。 E / flutter(17133):#0 _AssertionError._doThrowNew(dart:core / runtime / liberrors_patch.dart:37:39)E / flutter(17133):#1 _AssertionError._throwNew(dart:core / runtime / liberrors_patch.dart:33:5)E / flutter(17133):#2 TransitionRoute.install(package:flutter / src / widgets / routes.dart)E / flutter(17133):#3 ModalRoute.install(package:flutter / src / widgets / routes.dart:740:11)E / flutter(17133) ):#4 NavigatorState.push(包:flutter / src / widgets / navigator.dart:1444:11)E / flutter(17133):#5 RandomWordsState.build._popupMenuSelected(file:/// D:/ Flutter%20Projects /startup_namer/lib/main.dart:166:29)E / flutter(17133):#6 _PopupMenuButtonState.showButtonMenu。 (包:flutter / src / material / popup_menu.dart)E / flutter(17133):#7 _RootZone.runUnary(dart:async / zone.dart:1381:54)E / flutter(17133):#8 _FutureListener.handleValue(dart:async / future_impl.dart:129:18)E / flutter(17133):#9 Future._propagateToListeners.handleValueCallback(dart:async / future_impl.dart:633:45)E / flutter(17133):#10 Future._propagateToListeners(dart:async / future_impl.dart:662:32)E / flutter(17133):#11 Future._completeWithValue(dart:async / future_impl.dart:477:5)E / flutter(17133):#12 Future._asyncComplete。 (dart:async / future_impl.dart:507:7)E / flutter(17133):#13 _microtaskLoop(dart:async / schedule_microtask.dart:41:21)E / flutter(17133):#14 _startMicrotaskLoop(dart:async / schedule_microtask.dart:50:5)

答案

你有没有尝试过:

void _popupMenuSelected(PopupMenuChoice choice) {
  Navigator.push(context, choice.pageRoute);
}
另一答案
    void _popupMenuSelected(PopupMenuChoice choice) {
      await Future.delayed(const Duration(milliseconds: 100));
      Navigator.push(context, choice.pageRoute);
    }

以上是关于颤动:使用navigator.dart时失败断言的主要内容,如果未能解决你的问题,请参考以下文章

测试小部件时在颤动中测试 BlocListener 失败

构建失败并出现异常 - 颤动

发布构建失败颤动

使用 Opencv 时 C++ 调试断言失败

当我使用添加函数时断言失败

在 PathParam 中使用 PathSegment 测试 POST 请求时断言失败