Flutter popUntil引发错误状态:将来已经完成 [英] Flutter popUntil throws Bad state: Future already completed
问题描述
我有屏幕A-> B-> C-> D
I have screens A->B->C->D
在B,C,D屏幕中,有一个按钮可以带您到屏幕A 保持其状态(因此,pushNamedAndRemoveUntil在这里不合适)。
In B, C, D screens there is a button that should take you to screen A keeping it's state (thus pushNamedAndRemoveUntil isn't appropriate here).
我想使用popUntil,基于文档,这就是我的操作方式:
I want to use popUntil, that's how I do it, based on docs:
Navigator.popUntil(context, ModalRoute.withName(ScreenName.mainScreen));
我得到一个错误:
错误的状态:将来已经完成
I get an error: Bad state: Future already completed
这是我的主要对象:
void main() {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
final pages = {
ScreenName.mainScreen: (settings) => MaterialPageRoute(
builder: (context) => MainScreen(), settings: settings),
};
var configureApp = AppConfig(
appName: 'TaskerMate',
flavorName: FLAVOR_NAME.PROD,
child: AppModelProvider(
model: AppModel(),
child: MaterialApp(
theme: TMTheme().get(),
home: SplashScreen(),
onGenerateRoute: (settings) {
pages[settings.name](settings);
},
routes: {ScreenName.mainScreen: (context) => MainScreen()},
),
),
);
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((LogRecord rec) {
print('${rec.level.name}: ${rec.time}: ${rec.message}');
});
runApp(configureApp);
}
ScreenName.mainScreen->静态最终字符串mainScreen ='/ main';
ScreenName.mainScreen -> static final String mainScreen = '/main';
推荐答案
我花了很长时间才能找到答案,但是如果有人发现自己陷于这个问题,请RémiRousselet回答的另一个问题是为我解决了什么:
Took me ages to find the answer, but if anyone finds themselves stuck with this problem, Rémi Rousselet's answer to another question is what solved it for me:
Navigator.pushReplacement(
context,
MaterialPageRoute(settings: RouteSettings(name: "Foo")),
);
使用名称向MaterialPageRoute添加设置,然后像这样调用popUntil:
Add settings to MaterialPageRoute with name, then call popUntil like so:
Navigator.popUntil(context, ModalRoute.withName("Foo"))
这篇关于Flutter popUntil引发错误状态:将来已经完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!