Equivalent of Navigator.pushNamed with Navigator 2.0 in Flutter? - flutter

First: forgive me if it's a dumb question, but I'm trying to figure out step by step how Navigator 2.0 works and how to implement it in my app; this seems to be the only piece left of this puzzle but I weren't able to find an answer anywhere.
I've followed this Medium article on how to implement the new Navigator inside my Flutter app but I don't get how to use it after I've finished.
I mean: I have replaced MaterialApp with MaterialApp.router, specifying the custom RouterDelegate and the custom RouteInformationParser I've created, but how can I switch from the '/home' route to the '/profile' one after I pressed a button?
With the old navigator I would have added a Navigator.of(context).pushNamed('/profile) in the onPressed function of the button in the home screen, is it still valid or now should I proceed different?

For navigator 1.0:
Navigator.of(context).pushNamed('/profile');
For navigator 2.0 (using go_router package):
GoRouter.of(context).pushNamed('/profile');

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

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.

Why is there no back button in the Flutter Navigator 2.0 Example?

as I started working on a Flutter application I came across the Navigator 2.0. As the Documentation suggested, I used the code provided in the Documentation by John Ryan: https://medium.com/flutter/learning-flutters-new-navigation-and-routing-system-7c9068155ade.
My Problem is that the example without a nested navigation has an automatically displayed back button but the example with nested navigation does not. Why is that the case and is there a way to let the back button be automatically generated?
I am using the Flutter 1.25.0-8.2.pre beta channel.
Thank you in advance!
Your handler should indicate that the enclosing route should not be closed, hence returning false will resolve your issue.
changing your handler to this works:
onWillPop: () async {
navigatorKey.currentState.maybePop();
return false;
},
I could not really solve this issue. As far as I can see, it has something to do with the nested Router delegates. However, I have implemented a flattened structure which uses only one router delegate and it works fine.

OnNavigatedTo only gets called when using NavigationService

We have a MVVM app build with Xamarin.Forms and Prism.Forms. The OnNavigatedTo and OnNavigatedFrom (from INavigationAware) only seem to get called when navigating using Prism's NavigationService.
When using the hardware backbutton or the backbutton in the appbar the OnNavigatedTo and OnNavigatedFrom methods do not get called.
Is this a bug or am I missing something? When you can not rely on it, what is the use of INavigationAware?
This is a limitation of the current Xamarin.Forms platform: https://github.com/PrismLibrary/Prism/issues/744