首页 > 解决方案 > Flutter Navigator:断言失败... !_debugLocked 在 Navigator.pushNamed() 上不正确

问题描述

好吧,假设我有四个屏幕,Main、Login、Home 和 Item。我想要的“用户流程”是:Main -> Login(登录设置为initialRoute)-> Home -> Item。我可以轻松进入主屏幕,但随后我收到以下消息:Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4112 pos 12: '!_debugLocked': is not true.我找到了很多解决方案,但没有一个有效。结束了这些代码:

  1. 主要的
return MaterialApp(
  title: 'LetBike',
  theme: ThemeData(
    textTheme:
        GoogleFonts.josefinSansTextTheme(Theme.of(context).textTheme),
    primarySwatch: Colors.blue,
    visualDensity: VisualDensity.adaptivePlatformDensity,
  ),
  initialRoute: "/",
  routes: {
    "/": (context) => LoginScreen(),
    "ForgotPassword": (context) => ForgotPassword(),
    "CreateNewAccount": (context) => CreateNewAccount(),
    HomePage.routeName: (context) => HomePage(),
    ItemPage.routeName: (context) => ItemPage()
  },
);
}
  1. 登录
Navigator.of(context).pushReplacementNamed(
HomePage.routeName,
arguments: snapshot.data);
Navigator.of(context)
.pushNamed(ItemPage.routeName, arguments: item);

有任何想法吗?

标签: flutterdartstack-navigator

解决方案


What if you use

"WidgetsBinding.instance.addPostFrameCallback((_) {
      Navigator.of(context).popAndPushNamed(_routeName);"

instead of "Navigator.of(context).pushReplacementNamed(_routeName)"


推荐阅读