Flutter:使用navigator.dart时断言失败

mae*_*i70 10 dart flutter

我是Flutter的新手,正在玩它。所以,请耐心等待我。

单击PopupMenuButton的特定菜单项时会引发以下异常,但始终仅是第二次:

'package:flutter / src / widgets / navigator.dart':失败的断言:1846行pos 12:'!_debugLocked':不正确。

这里的设置:

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

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

  final String title;
  final MaterialPageRoute pageRoute;
}
Run Code Online (Sandbox Code Playgroud)

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,
),
Run Code Online (Sandbox Code Playgroud)

在下面的类中定义了相应的Widget(在该类的“返回新支架”中创建AppBar):

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

  ...
}
Run Code Online (Sandbox Code Playgroud)

如您所见,有用于保存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));
    }
    ...
Run Code Online (Sandbox Code Playgroud)

在PopupMenuChoice的已定义MaterialPageRoute中,可以访问私有变量(例如_saved)。

以下是PopupMenuButton的onSelected的相应事件处理程序:

void _popupMenuSelected(PopupMenuChoice choice) {
  Navigator.of(context).push(choice.pageRoute);
}
Run Code Online (Sandbox Code Playgroud)

谁能解释为什么抛出此异常?以及如何预防呢?

谢谢罗杰


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

E / flutter(17133):[错误:topaz / lib / tonic / logging / dart_error.cc(16)]未处理的异常:E / flutter(17133):“ package:flutter / src / widgets / routes.dart”:失败断言:行177位置12:'!_transitionCompleter.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)

小智 5

    void _popupMenuSelected(PopupMenuChoice choice) {
      await Future.delayed(const Duration(milliseconds: 100));
      Navigator.push(context, choice.pageRoute);
    }
Run Code Online (Sandbox Code Playgroud)


小智 2

你有没有尝试过:

void _popupMenuSelected(PopupMenuChoice choice) {
  Navigator.push(context, choice.pageRoute);
}
Run Code Online (Sandbox Code Playgroud)