Search code examples
flutterdartstack-navigator

Flutter Navigator: Failed assertion ... !_debugLocked is not true on Navigator.pushNamed()


Well, let's say I have four screens, Main, Login, Home and Item. My desired "user-flow" is: Main -> Login (login is set as initialRoute) -> Home -> Item. I can get to the Home screen easily, but then I get this message: Another exception was thrown: 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 4112 pos 12: '!_debugLocked': is not true. I have found a looot of solutions, but none of them worked. Ended up with these codes:

  1. Main
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. Login
Navigator.of(context).pushReplacementNamed(
HomePage.routeName,
arguments: snapshot.data);
  1. Home
Navigator.of(context)
.pushNamed(ItemPage.routeName, arguments: item);

Any ideas?


Solution

  • What if you use

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

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