go_router using browser back button extra is null flutter web - flutter

let's say I navigate from page a with context.push() and pass extra to page b.
then if I navigate to page c and then press the browser back button to go back to page b now extra is null and page shows error.
any solutions?
I think we can use query parameters but it brings up security problems.

You can follow one of the two possible solution:
Dont let page to come back to B from C, You can use something like popUntil
When coming back to B from C override the naviagetion such that you send the extra object along with it. So that B has extra value
Edit:
Presently there is no way to achieve this using go_router. You can use go_router_flow which is exactly like go_router with this pop with value feature.
final bool? result = await context.push<bool>('/page2');
WidgetsBinding.instance.addPostFrameCallback((_) {
if(result){
print('Page returned $result');
}
});

Use the beamer package for routing.This problem occur in the go router.
beamer package link

Related

How to update state when Navigating back to a page Flutter

i am creating a social media like app, when user clicks on post ,it redirects to a commentpage,in commentspage data is coming from a seperate API,so when go back using Navigator.pop(context), the data in home is not updated, if pressed like button,its not reflecting in ui, but updating in Api,
i wrapped Navigator with setstate but its not reflecting,
Use this on navigator push Navigator.push(context).then((value)=>setState((){}); when it pop the screen it will run the setState
Use this sentence to go to the second page so the function will wait second screen to close
bool result = await Navigator.push(context);
if(result){setState((){});}
when u close the second screen use this sentence Navigator.pop(context, true);
so when u comment has been posted successfully, u will back to screen one and complete the function if result boolean is true, else do nothing.

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

flutter- getx nested routes with tab

I am going to build flutter app with Getx.
I followed nested navigator from
https://github.com/jonataslaw/getx/issues/799#issuecomment-730719165
at this example, only shows 1 step sub routes like "/browse".
I need to make another sub routes like "/browse/tabX" and "/browse/tabY"
when click browse button from BottomNavigationBar, It shows tabs (tabX and tabY) on the top. and tabX is default selected.
for the body, it shows automatically their pages.
Also using Get.Named('/browse/tabY') can access to open their page(select second tab and show their body).
Can you help me?
Oh Now I got the answer!
What I make a mistake is I did not use "sub-name".
for example, at initialRoute,
I used '/home/browser' but it should be '/browser' if it is sub-route.
Because the concept is route to subrouter's root and handle by subrouter.
so "initialRoute from root" route to /home first.
and "initialRoute from subroute" route to /browser again!
I use classed variable instead just string so I did not figure this difference out.

Flutter: Update specific screen using provider that's consumed in multiple screens

I have a typical CRUD operation task: (List Sites, Add Site, ...)
I created a SitesProvider. In the same provider, I added 2 methods for add and edit. I was thinking to use the same provider in the 2 screens (ListSites and AddEditSite)
In the ListSites screen, it works fine. Here is the problem:
I open the AddSite screen by clicking the AddButton in the ListSites screen
Hit submit (did not enter data, simulating error case)
The error gets displayed in the ListSites screen, not in the AddSite screen.
It makes sense. They both use the same provider, both screens are on the stack. It seems that the first one only consumes the state update and displays the error.
I use MultipleProviders approach that wraps the MaterialApp with all providers in the app.
Can we fix that without creating separate providers for each of the 2 screens?
EDIT:
I used provider.removeListener in the ListSites screen right before I open the AddEdit and it shows the error in the correct screen now. I still have to do some other tweaks to get it back to listen after I add. Not efficient I think but it is a step.
I ended up doing that:
Added Another Field in the provider (stateType: list/addedit)
Change the type per the screen I'm currently in
provider.stateType = UIStateType.add_edit;
await Navigator.push(context,
MaterialPageRoute(builder: (context) => AddEditSiteScreen()));
provider.stateType = UIStateType.list;
in build(), I check for the type
sitesProvider = Provider.of(context);
if (sitesProvider.stateType != UIStateType.add_edit) return Container();

Apply method immediately after returning to page in flutter

I have two pages: page A has a "search" function, page B has a "select language" function. I navigate from page A to page B to select my language. Then I want to pop back from page B to page A with the new chosen language and perform the search automatically for that language.
I pass my chosen language from Page B to A by using SharedPrefs (which is working fine). So all I need is when I return to Page A, for the search function to get automatically triggered.
Is that possible in Flutter?
I tried using initState but that only works once when the widget is first built. I also tried didUpdateWidget but nothing happened.
Use this when navigating to PageB from PageA
Navigator.push(context, MaterialPageRoute(builder:(_) => PageB())).then((value) {
// you have come back to the pageA, now perform your logic
callThisMethod();
});