csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 97 forks source link

Simpler way to get previous location #237

Closed superakabo closed 2 years ago

superakabo commented 2 years ago

It is very easy to get the current location by doing

GoRouter.of(context).location;

It will be nice to have a simpler implementation for the previous location(s) as well by doing

GoRouter.of(context).previousLocations;
lulupointu commented 2 years ago

Building some logic around a previousLocation can be tricky. Here is why: what happens when the browser back/forward button is pressed?

superakabo commented 2 years ago

Building some logic around a previousLocation can be tricky. Here is why: what happens when the browser back/forward button is pressed?

It shouldn't be updated until a new/different route is pushed.

lulupointu commented 2 years ago

It shouldn't be updated until a new/different route is pushed.

Mmm I'm not sure this is desirable. My question was rather between (in the case of pressing the back button):

Also the behavior described above if for a simple back button press but mind that on the web user can do really what they want.

Here is another interesting question: what happens if the user reload the page on the web? The app is restarted so the state is lost therefore previousLocations has to be empty right ? This is a weird and unexpected behavior (if you try to rely on this to display the unique button to get out of the page for example).

I'm not saying it's not possible. Choices can be made and a system could work. The issue is that what you end up would not be very intuitive and would behave weirdly in certain edge cases (which the dev might not take into account before a user run into the issue, which is the worse kind of bugs)

superakabo commented 2 years ago

It shouldn't be updated until a new/different route is pushed.

Mmm I'm not sure this is desirable. My question was rather between (in the case of pressing the back button):

* Adding the new url and appending the previous to `previousLocations`

* Removing the last element of `previousLocations` <- This is basically how browser history work

Also the behavior described above if for a simple back button press but mind that on the web user can do really what they want.

Here is another interesting question: what happens if the user reload the page on the web? The app is restarted so the state is lost therefore previousLocations has to be empty right ? This is a weird and unexpected behavior (if you try to rely on this to display the unique button to get out of the page for example).

I'm not saying it's not possible. Choices can be made and a system could work. The issue is that what you end up would not be very intuitive and would behave weirdly in certain edge cases (which the dev might not take into account before a user run into the issue, which is the worse kind of bugs)

OK.

csells commented 2 years ago

@superakabo this functionality should be accessible to you by passing in a NavigatorObserver object. You can see that in action in the following example: https://github.com/csells/go_router/blob/main/go_router/example/lib/nav_observer.dart