Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error thrown on navigator pop until : "!_debugLocked': is not true."

When popping a screen navigating to other one by clicking on the showBottomSheet, this error is thrown through the following code . I cant get why this is occurring.

class _CheckoutButtonState extends State<_CheckoutButton> {   final GlobalKey<ScaffoldState> _globalKey = GlobalKey();   final DateTime deliveryTime = DateTime.now().add(Duration(minutes: 30));    final double deliveryPrice = 5.00;    @override   Widget build(BuildContext context) {     SubscriptionService subscriptionService =         Provider.of<SubscriptionService>(context);     CheckoutService checkoutService = Provider.of<CheckoutService>(context);     return Container(       height: 48.0,       width: MediaQuery.of(context).size.width * 0.75,       child: StreamBuilder(         stream: subscriptionService.subscription$,         builder: (_, AsyncSnapshot<Subscription> snapshot) {           if (!snapshot.hasData) {             return Text("CHECKOUT");           }           final Subscription subscription = snapshot.data;           final List<Order> orders = subscription.orders;           final Package package = subscription.package;           num discount = _getDiscount(package);           num price = _totalPriceOf(orders, discount);           return StreamBuilder<bool>(               stream: checkoutService.loading$,               initialData: false,               builder: (context, snapshot) {                 bool loading = snapshot.data;                 return ExtendedFloatingActionButton(                   loading: loading,                   disabled: loading,                   action: () async {                     checkoutService.setLoadingStatus(true);                     final subscription =                         await Provider.of<SubscriptionService>(context)                             .subscription$                             .first;                     try {                       await CloudFunctions.instance.call(                           functionName: 'createSubscription',                           parameters: subscription.toJSON);                       final bottomSheet =                           _globalKey.currentState.showBottomSheet(                         (context) {                           return Container(                             width: MediaQuery.of(context).size.width,                             decoration: BoxDecoration(                               gradient: LinearGradient(                                 begin: Alignment.topCenter,                                 end: Alignment.bottomCenter,                                 colors: [                                   Theme.of(context).scaffoldBackgroundColor,                                   Theme.of(context).primaryColor,                                   Theme.of(context).primaryColor,                                 ],                                 stops: [-1.0, 0.5, 1.0],                               ),                             ),                             child: Column(                               children: <Widget>[                                 Expanded(                                   child: Column(                                     mainAxisAlignment: MainAxisAlignment.center,                                     children: <Widget>[                                       Padding(                                         padding:                                             const EdgeInsets.only(bottom: 16.0),                                         child: Text(                                           "Thank you for your order",                                           textAlign: TextAlign.center,                                           style: Theme.of(context)                                               .textTheme                                               .display1,                                         ),                                       ),                                       SvgPicture.asset(                                         'assets/images/thumb.svg',                                         height: 120.0,                                         width: 100.0,                                       )                                       // CircleAvatar(                                       // radius: 40.0,                                       // backgroundColor: Colors.transparent,                                       // child: Icon(                                       // Icons.check,                                       // color: Theme.of(context)                                       // .textTheme                                       // .display1                                       // .color,                                       // size: 80.0,                                       // ),                                       // ),                                     ],                                   ),                                 ),                                 Container(                                   width:                                       MediaQuery.of(context).size.width * 0.9,                                   height: 72.0,                                   padding: EdgeInsets.only(bottom: 24),                                   child: ExtendedFloatingActionButton(                                     text: "ORDER DETAILS",                                     action: () {                                       Navigator.of(context).pop();                                     },                                   ),                                 ),                               ],                             ),                           );                         },                       );                       bottomSheet.closed.then((v) {                         Navigator.of(context)                             .popUntil((r) => r.settings.isInitialRoute);                       });                     } catch (e) {                       print(e);                       final snackBar =                           SnackBar(content: Text('Something went wrong!'));                       Scaffold.of(context).showSnackBar(snackBar);                     }                   },                   child: Row(                     mainAxisAlignment: MainAxisAlignment.center,                     children: <Widget>[                       Text(                         "CHECKOUT ",                         style: Theme.of(context)                             .textTheme                             .display4                             .copyWith(color: Colors.white),                       ),                       Text(                         "EGP " +                             (price + (orders.length * deliveryPrice))                                 .toStringAsFixed(2),                         style: Theme.of(context)                             .textTheme                             .display4                             .copyWith(color: Theme.of(context).primaryColor),                       ),                     ],                   ),                 );               });         },       ),     );   }    num _totalPriceOf(List<Order> orders, num discount) {     num price = 0;     orders.forEach((Order order) {       List<Product> products = order.products;       products.forEach((Product product) {         price = price + product.price;       });     });     num priceAfterDiscount = price * (1 - (discount / 100));     return priceAfterDiscount;   }    num _getDiscount(Package package) {     if (package == null) {       return 0;     } else {       return package.discount;     }   } }  

Error :

>══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════ I/flutter (24830): The following assertion was thrown building Navigator-[GlobalObjectKey<NavigatorState> I/flutter (24830): _WidgetsAppState#90d1f](dirty, state: NavigatorState#6b2b6(tickers: tracking 1 ticker)): I/flutter (24830): 'package:flutter/src/widgets/navigator.dart': Failed assertion: line 1995 pos 12: '!_debugLocked': I/flutter (24830): is not true. I/flutter (24830): Either the assertion indicates an error in the framework itself, or we should provide substantially I/flutter (24830): more information in this error message to help you determine and fix the underlying cause. I/flutter (24830): In either case, please report this assertion by filing a bug on GitHub: I/flutter (24830):   https://github.com/flutter/flutter/issues/new?template=BUG.md I/flutter (24830): When the exception was thrown, this was the stack: 
like image 450
Aya Elsisy Avatar asked Apr 10 '19 17:04

Aya Elsisy


People also ask

What does Navigator POP do in Flutter?

Pop the top-most route off the navigator that most tightly encloses the given context. The current route's Route. didPop method is called first. If that method returns false, then the route remains in the Navigator's history (the route is expected to have popped some internal state; see e.g. LocalHistoryRoute).

How do you fix navigator operation requested with a context that does not include a Navigator?

Navigator operation requested with a context that does not include a Navigator. this error comes by inserting in your widget tree a MaterialApp or WidgetApp widget. Try adding MaterialApp widget in main method and remaining code in stateless or stateful widget then issue will be resolved.

What is navigator pushReplacement in Flutter?

pushReplacement<T extends Object, TO extends Object> methodReplace the current route of the navigator by pushing the given route and then disposing the previous route once the new route has finished animating in.


1 Answers

Instead of giving you a direct answer, I'm going to walk you through how I thought about this when I saw the question, in the hope that it'll help you in the future.

Let's take a look at the assertion. It says Failed assertion: line 1995 pos 12: '!_debugLocked': I/flutter (24830): is not true.. Hmm, interesting. Let's take a look at that line of code.

assert(!_debugLocked);

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

That's better. It's there to prevent re-entrant calls to push, pop, etc (by that it means that it doesn't want you calling 'push', 'pop', etc from within a call to 'push', 'pop'). So let's trace that back to your code.

This seems like the likely culprit:

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

I'm going to skip a step here and use deductive reasoning instead - I'm betting that the closed future is finished during a pop. Go ahead and confirm that by reading the code if you feel like it.

So, if the issue is that we're calling pop from within a pop function, we need to figure out a way to defer the call to pop until after the pop has completed.

This becomes quite simple - there's two ways to do this. The simple way is to just use a delayed future with zero delay, which will have dart schedule the call as soon as possible once the current call stack returns to the event loop:

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

The other more flutter-y 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 should eliminate the problem you're having.

Another option is also possible though - 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 the doing anything after bottomSheet.closed is called. However, depending on whatever else you might or might not need to do in your logic this may not be ideal (I can definitely see the issue with having the bottom sheet set off a change to the main part of the page and why you've tried to make that happen in the page's 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. 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 wherever possible - you can generally either pass objects (or callbacks) down if it's only through a few layers, use the .of(context) pattern, or use inherited widgets.

like image 66
rmtmckenzie Avatar answered Sep 25 '22 00:09

rmtmckenzie