Navigate depending on value flutter - flutter

He there
I just face a problem with this code error ->
Error thrown on navigator pop until : "!_debugLocked': is not true."
What the Scenario I do?
I use redux as state management, I check variable content if it's not null then show the dialog (I don't do that on click or tap listener)
Any suggestions :(
Thanks in advance.

Related

Flutter - How to check if dialog is shown when using go_router

I was using Navigator 1 then I migrated to go_router to support deep links and web.
Sometimes when I send HTTP requests, I show a loading dialog using showDialog() until the response is processed, after processing I need to check if a dialog is shown or not, if shown I dismiss it using Navigator.of(context).pop().
When I was using Navigator 1, I used to do that in this way:
if (ModalRoute.of(context)?.isCurrent == false) Navigator.of(context).pop();
But now after migrating to go_router this doesn't work as I found that ModalRoute.of(context) always equals to null whether there is a dialog shown or not.
I tried using if (Navigator.of(context).canPop()) Navigator.of(context).pop();, but this doesn't differentiate between dialogs and screens, if there is no dialog shown it pops the current screen.
You can do this by checking the location of GoRouter
if (GoRouter.of(context).location == '/dialog'){ 👈 Check here
context.pop();
}
Assuming your dialog route has path /dialog

Unable to Navigate to the profile screen in the AuthStateChange callback as shown in docs

I am using firebase_ui_auth package for authentication in my first Flutter app using the code below. I am only using phone authentication provider. The problem I am facing is after I enter the OTP and click verify AuthStateChangeAction callback is triggered, inside this callback I want to navigate to the profile screen so I use the below line of code as mentioned in the documentation.
Navigator.pushReplacementNamed(context, '/profile');
But the above line doesn't takes me to the profile screen, instead after I enter the OTP and click verify it takes me again to the sign-in screen. So, I took a Codementor session and he changed the line in the AuthStateChangeAction callback to the below line.
WidgetsBinding.instance.addPersistentFrameCallback((timeStamp) {
Navigator.pushReplacementNamed(context, '/profile');
});
This takes me to the profile screen(expected behaviour) after I enter the OTP and click Verify button. But now in the logs, I am getting the below error.
Another exception was thrown: Looking up a deactivated widget's ancestor is unsafe.
So, I wanted to know answers to below questions?
what does using WidgetsBinding.instance.addPersistentFrameCallback do?
How does it affect the screen stack of my app?
Why deactivated widget's ancestor exception is raised?
Why am I not navigated to the profile screen when I don't use WidgetsBinding?
Is it fine to use WidgetsBinding for navigating to other screens?
What is the best practice for navigation to the profile screen?

pop until in flutter auto route package does not work

I am using the AutoRoute package for Flutter.
I have the following two screens generated using the auto route package,
HomeRoute()
ProductsRoute()
Then from HomeRouteI do the following,
context.router.push(const ProductsRoute());
Then inside the ProductRoute I call an API in the initState and if I an error occurs, I show a pop up that says Something went wrong!. Here,
I want to pop the alert, then pop the ProductsRoute() so that the user navigates back to the HomeRoute().
So using the AutoRoute I did the following,
context.router.popUntil((route) => route.settings.name == 'HomeRoute')
This did not work. It leads me to a white screen.
However it does work if I do context.route.pop() twice.
Can someone please tell me what I am doing wrong and how can I navigate back to the HomeRoute() using the Auto Route package?
Thanks.
context.router.popUntil((route) => route.name == 'HomeRoute')
or
context.router.popUntilRouteWithName('HomeRoute')
I found the problem,
With popUntil, if I want to go to the HomeRoute, I should not mention the HomeRoute. Instead I should mention the route on the stack that is after the HomeRoute. Then it started to work,
For example suppose I have the following stack,
ScreenA -> ScreenB -> ScreenC -> ScreenD
Now if I want the user to show screen A by popping all the screens from screen D,
I should do,
context.router.popUntilRouteWithName(ScreenBRoute.name); // <---- Should do
NOT
context.router.popUntilRouteWithName(ScreenARoute.name); // <---- Should not do

How can I identify which component is causing a Flutterflow Page Error when loading?

Within Flutterflow
When moving between pages with a navigate action, with variables passed between screens, the destination screen breaks and shows a grey page.
Variables being passed are an amount double, an image path. a string, and an account document object reference.
The screen loads a drop down menu with a list of string values.
The Console Log does not show a lot of information about what went wrong, where the page transition is failing, or which variable is causing issues.
Output when the screen loads is:
Uncaught TypeError: [object Object] is not a function
at Array.map (<anonymous>)
at a0.b8 (main.dart.js?1652057862:88960:18)
at Object.aM (main.dart.js?1652057862:79323:26)
at a0M.eX (main.dart.js?1652057862:96693:10)
at e_k.$1 (main.dart.js?1652057862:177146:8)
at buu.wJ (main.dart.js?1652057862:92138:33)
at die.$0 (main.dart.js?1652057862:91234:11)
at Object.av5 (main.dart.js?1652057862:5177:40)
at bh.o0 (main.dart.js?1652057862:91147:3)
at ceq.$0 (main.dart.js?1652057862:91006:16)
Some guidance would be great, or a way to add console.log() debugging into the flow of actions to break down what the issue is.
If you did not try yet - there is a debug mode which should give you more information on the exceptions. Instead of run select the dropdown next to it and select test.

Failed assertion: !_debugLocked is not true when using Navigator.popUntil() in Flutter

In my flutter app I use this code to go to my dashboard page, but it fails to do so the first time and I get the debugLocked error.
The launch page in the app is a Router that checks for shared preferences "sessionid" which, if set, takes the user directly to the Dashboard, else takes them to the login.
Without the below code, I get to my Dashboard just fine, using Navigator.pushReplacement() but then, a back arrow appears on the appBar. This back button takes the app back to the Router.
I searched for answers on how to remove all screens from the navigator and the following was what I found.
Navigator.of(context).popUntil(ModalRoute.withName(Dashboard.id));
Using the above code gives me the debugLocked error. Is there a solution to mitigate this problem? Or is there any other efficient way for me to remove screens from the context? Does setting automaticallyImplyLeading to false help somehow? Because this error only occurs after someone has logged in or signed up.
to remove all the previous routes use Navigator.pushAndRemoveUntil()
This can be another alternative, if you for some reason face a setState error.
navigationService.popUntil((_) => true);
navigationService.navigateTo(
'authentication',
);
basically i wait until the navigation finish setting everything and then call the navigateTo.