How to see GoRouter navigation stack's top element without poping it? - flutter

I am building a flutter app. I have a place in the app where you can make infinite loops with
GoRouter.of(context).push(...);
I mean you can navigate to Screen A and then to B and then to A. There are many screens and many possible loops but I want to protect the user from the most obvious one, the A>B>A>B type.
(I don't care about greater loops like A>B>C>A>B>C and alikes)
To do that, I want to check the navigation stack top element and if the element is the same where we would go with a push, I would do a pop.
Problem is that I can't find anything on Google...
Tried:
Googling it, checking API docs, reading IntelliSense
EDIT:
There is a big set of screens and can't know the previous location solely based on the current one.

Use GoRouter.of(context).location
GoRouter.of(context).location gives you the current location
Example:
Route A has route path /routeA
Route B has route path /routeB
GoRouter.of(context).location will return either /routeA or /routeB
Based on which you can make decision to pop() or push() page

Related

Flutter - Keep track of data between screens

I'm trying to make a Gym app. You can add a workout by pressing the + on the appbar, and this takes you to a new screen where you can add the information about all the exercises and the workout name. When you press the check button, it goes back to the main screen where it displays all the workout that you've created, so that if you tap on a list tile it displays all the exercises.
My problem is that I don't know how to pass all the information about the exercises back to the main page. The only thing that I pass back is the workout name. My idea was to pass a Map<String workoutName, List> so that in the main page I have everything that I need. What do you think about it?
P.S. Rn I'm not storing anything in LocalStorage yet, mainly because I don't know what to store I was thinking about storing the Map<String workoutName, List> But I'm a fresh dev on Flutter so there may be easier solutions.
There are many ways to do this. Because of the inevitable growth of your requirements, I suggest going with the most common way to both pass variables and control your state. Meaning: when your variables change, when you return to your original screen, the screen also rebuilds to show your new information. Riverpod has become the successor to the previously Flutter team recommended State Management solution.
I suggest finding a nice, popular tutorial on Riverpod, perhaps building an app entirely with the video to give yourself a good start.

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 the navigation stack hidden in Flutter?

Why is the navigation stack private in Flutter? I am curious as to why the Flutter authors decided to make it hidden as a design choice.
For those interested, the navigation history is referenced as '_history' within NavigtorState.
class NavigatorState extends State<Navigator> with TickerProviderStateMixin, RestorationMixin {
late GlobalKey<OverlayState> _overlayKey;
List<_RouteEntry> _history = <_RouteEntry>[];
final _HistoryProperty _serializableHistory = _HistoryProperty();
Side notes (looking for a response to above question):
Right now I am making a navigation observer that keeps track of the navigation stack using a queue. When push, pop, or remove are called, I update my queue accordingly. I solely use this to keep track of the depth of the navigation stack. This stack depth, combined with WillPopScope allows for me to fine tune when I want a user to be able to pop or not. Without getting into too much detail, I want my selected tab to not be popable to the root route (route: '/'). By knowing the depth is of length 1, I can use WillPopScope to stop a user from popping.
This circles me back to the original question; is keeping track of the stack a bad design choice for Flutter?
I have found limit resources as to WHY the flutter devs chose to make it private.
My one thought is to prevent users from manually editing the history. So why not allow for a public function such as getHistoryLength()?
Many thanks,
-Flutter Newbie

Root route and different initialRoute creates strange WidgetTree

I'm trying to navigate between different screens using named routes.
I'm defining a root-Route (you may call it fallback route(?)), some other routes and an initialRoute, linking to a different screen than the root-Route.
The widget tree seems to load the root-route as well. But why?
TicketsScreen has many Widgets and I don't want them to be loaded beforehand.
BTW: This is only an example. When having multiple routes, it still loads both of the mentioned.
That's because /tasks has a leading /.
The navigation system pushes everything that's there, let me explain.
If you had there routes:
/
/tasks
/tasks/new
navigating to /tasks/new will push all of three.
If you want to keep "single" routes, you should use top level qualifiers. In your case that would be removing the / from /tasks.
This mechanism is useful to push a path and avoid strange pops if, for example,
you navigate to /tasks/new from a shortcut (not from /tasks) and then pop back. Would it be good to pop to the starting point? Would it be better if popping from a new task will lead to the tasks page?
That's a brief explanation of what the navigator tries to do, I guess.

Supress visible screen switching on iPhone

In the iPhone application I'm developing, I have a need to return the user to the previous screen they were using when, for instance, the application was interrupted by, say, a phone call.
The application is driven by a navigation controller, and the user may be several layers deep into the application. As such, I think that I need to traverse the navigation controller logic to bring the user to the point that they were previously at, with all return navigation logic n place.
I'm comfortable that I can force the application to navigate down to the required level through code, but I would like to hide the screen switching and animations that would occur while this is going on, thus presenting the user with (apparently) a direct path to their last used screen, rather than showing them the underlying navigation that's occurred.
Could somebody please point me to some method of suppressing the intermediate displays?
Does anyone have other methods to perform this sort of task?
Thanks in advance for all suggestions.
I suggest you take a look at the Three20 project which contains a feature called "URL-based navigation", which might help you, since you only should to store the URL of the current visible view controller, and restore it when the app resumes after the phone call:
TTNavigationCenter is for those grizzled old web developers like myself who want to organize their app by "pages" which can be displayed by visiting a URL.
Your view controllers can simply register URL patterns that they handle, and when those URLs are visited the controllers will be created and displayed. You can also register generic actions that are called when a URL is visited.
TTNavigationCenter also persists and restores the full path of navigation controllers and modal view controllers, so your users can quite the app and come back exactly where they left off.
(source: Three20 Github project)