我刚开始接触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列表在“build override”中设置:
@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));
}
...
在定义的PopupMenuItem的MaterialPageRoute中,可以访问私有变量(例如_saved)。
这里是PopupMenuButton的onSelected对应的事件处理程序:
void _popupMenuSelected(PopupMenuChoice choice) {
Navigator.of(context).push(choice.pageRoute);
}
有人能解释一下为什么会抛出这个异常吗?它如何避免发生?
感谢, Roger 点击特定菜单项第二次时,从调试控制台获取的其他信息:
[ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] 未处理的异常:'package:flutter/src/widgets/routes.dart': Failed assertion: line 177 pos 12: '!_transitionCompleter.isCompleted': Cannot install a MaterialPageRoute after disposing it.
#0 _AssertionError._doThrowNew (dart:core/runtime/liberrors_patch.dart:37:39) #1 _AssertionError._throwNew (dart:core/runtime/liberrors_patch.dart:33:5) #2 TransitionRoute.install (package:flutter/src/widgets/routes.dart) #3 ModalRoute.install (package:flutter/src/widgets/routes.dart:740:11) #4 NavigatorState.push (package:flutter/src/widgets/navigator.dart:1444:11) #5 RandomWordsState.build._popupMenuSelected (file:///D:/Flutter%20Projects/startup_namer/lib/main.dart:166:29) #6 _PopupMenuButtonState.showButtonMenu. (package:flutter/src/material/popup_menu.dart) #7 _RootZone.runUnary (dart:async/zone.dart:1381:54) #8 _FutureListener.handleValue (dart:async/future_impl.dart:129:18) #9 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:633:45) #10 Future._propagateToListeners (dart:async/future_impl.dart:662:32) #11 Future._completeWithValue (dart:async/future_impl.dart:477:5) #12 Future._asyncComplete. (dart:async/future_impl.dart:507:7) #13 _microtaskLoop (dart:async/schedule_microtask.dart:41:21) #14 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)