1

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.

Kolompos
  • 21
  • 3

1 Answers1

0

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

krishnaacharyaa
  • 14,953
  • 4
  • 49
  • 88
  • 1
    Sorry but the question was not exact enough. There is not only A and B but a huge set of routes and I don't know for certain what was the previous one based on the current one. This would only work if I had only A and B. Or if the chain was predefined. It is not. – Kolompos Jan 08 '23 at 22:26