颤动:使用navigator.dart时失败断言
我是Flutter的新手并且玩弄它.所以,请耐心等待我.
单击PopupMenuButton的特定菜单项时会抛出以下异常,但始终只是第二次:
这里设置: 为了指定菜单项,已经定义了以下类: 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); } 任何人都可以解释为什么抛出这个异常?它怎么能被阻止? 谢谢, 在特定菜单项上单击第二次时,调试控制台中的其他信息:
解决方法
你有没有尝试过:
void _popupMenuSelected(PopupMenuChoice choice) { Navigator.push(context,choice.pageRoute); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |