gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:32:42 28 4
gpt4 key购买 nike

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

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

'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1846 pos 12: '!_debugLocked': is not true.

这里是设置:

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

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,
),

对应的Widgets定义在如下类中(AppBar在该类的“return new Scaffold”中创建):

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

...
}

如您所见,有一些私有(private)变量用于保存 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 中,私有(private)变量可能被访问(例如 _saved)。

这里对应PopupMenuButton的onSelected的事件处理函数:

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

谁能解释为什么会抛出这个异常?以及如何预防?

谢谢,罗杰


第二次单击特定菜单项时来自调试控制台的附加信息:

E/flutter (17133): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception: E/flutter (17133): 'package:flutter/src/widgets/routes.dart': Failed assertion: line 177 pos 12: '!_transitionCompleter.isCompleted': Cannot install a MaterialPageRoute after disposing it. 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 (package: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. (package: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) {
await Future.delayed(const Duration(milliseconds: 100));
Navigator.push(context, choice.pageRoute);
}

关于dart - Flutter:使用 navigator.dart 时断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50117831/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com