How to Solve Error Thrown On Navigator Pop?

· 5 min read
How to Solve Error Thrown On Navigator Pop?
How to Solve Error Thrown On Navigator Pop?

When popping screen navigating to another one by clicking on the showBottomSheet and it start showing an error – Error Thrown On Navigator Pop Until: “!_debugLocked’: Is Not True”.

So in this article, we see how to solve this error.

How to Solve Error Thrown On Navigator Pop?

So, let’s take a look at the assertion. It says Failed assertion: line 1995 pos 12: ‘!_debugLocked‘: I/flutter (24830): is not true. Also, let’s take a look at that line of code.

assert(!_debugLocked);

But that doesn’t give me much more information, let’s look at the variable.

bool _debugLocked = false; // used to prevent re-entrant calls to push, pop, and friends

This is to prevent repeated push, pop, etc. calls (that is, you should avoid calling “push”, “pop”, etc. from “push”, “pop” calls). So, let’s trace that back to your code.

This seems to be the main problem:

bottomSheet.closed.then((v) {
  Navigator.of(context)
    .popUntil((r) => r.settings.isInitialRoute);
});

So, let’s skip a step here and use deductive reasoning instead – that the closed future is finished during a pop. Go ahead and confirm it by reading the code. Moreover, the simple way is to just use a delayed future with zero delays. When the current call log returns to the event loop, Dart will schedule the call as soon as possible.

Future.delayed(Duration.zero, () {
  Navigator. ...
});

But the other way of doing it would be to use the Scheduler to schedule a call for after the current build/render cycle is done:

SchedulerBinding.instance.addPostFrameCallback((_) {
  Navigator. ...
});

Either way, it should eliminate the problem you’re having. Also, another option is also possible through – in your ExtendedFloatingActionButton where you call pop:

ExtendedFloatingActionButton(
 text: "ORDER DETAILS",
  action: () {
    Navigator.of(context).pop();
  },
),

You could instead simply do the call to Navigator. of(context).popUntil….

That would eliminate the need for doing anything after bottomSheet.closed is called. However, this may not be ideal, depending on what else you need to do with your logic.

Also, when you’re writing your code I’d highly recommend separating it into widgets.

For example, the bottom sheet should be its own widget. So, the more you have in a build function, the harder it is to follow and it can actually have an effect on performance as well.

You should also avoid using GlobalKey instances as much as possible. Normally, if you have only a few layers of objects or callbacks, you can pass the objects or callbacks, use the .of (context) pattern, or use inherited widgets.

There is a similar error, like a dialog box that had a logout button, which when pressed goes to the login screen, but a _debugLocked error occurs, so we can use

Navigator.of(context).pushNamedAndRemoveUntil('/screen4', (Route<dynamic> route) => false);

This removes all routes in the stack so that the user cannot go back to the previous routes after they have logged out.

Setting (Route<dynamic> route) => false will make sure that all routes before the pushed route are removed.

In the Initial route which was/loading, the code was pushing /home

class _LoadingState extends State<Loading> {
  void getTime() async {
    // DO SOME STUFF HERE
    Navigator.pushNamed(context, '/home');
  }

  @override
  void initState() {
    super.initState();
    getTime();
  }

And in the /home initState() I was pushing /loading creating a cycle.

class _HomeState extends State<Home> {
  @override
  void initState() {
    super.initState();
    Navigator.pushNamed(context, '/loading');
  }

For those who are invoking the Navigator as part of the build process. I found that it will intermittently throw asserting error on the debut locked.

I avoided this issue by wrapping it with an addPostFrameCallback:

WidgetsBinding.instance.addPostFrameCallback((_) {
  Navigator.pushReplacement(context, MaterialPageRoute(builder: (_) => MyPage()));
});

Conclusion:

So, in this article, we have seen How to Solve Error Thrown On Navigator Pop? Do let us know your feedback/comments on the same. Flutter Agency is the best Flutter Application development company dedicated to Flutter Technology and Flutter App Developers. The portal is full of cool resources from Flutter like Flutter Widget Guide, Flutter Projects, Code libs and etc. So, contact us for your next project.

Leave a Reply