0

I'm using go_router_builder to build routes. Here's one:

class FooRoute extends GoRouteData {
  final bool condition;
  FooRoute(this.condition);
  
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return condition ? FooScreen() : null; // I wish this was possible!
  }
}

I want to keep the existing route (screen) if the condition is false. How can I do that?

void func() {
  FooRoute(true); // Navigates to the `FooScreen`. All good.
  FooRoute(false); // This should do nothing. 
}

Note:

I want to keep my logic in the FooRoute widget.

iDecode
  • 22,623
  • 19
  • 99
  • 186

1 Answers1

0

If you want to get the current route, you can do so using GoRouter.of(context).location getter method.

For example:

final currentLocation = GoRouter.of(context).location;
print(currentLocation) // "/HomePage/Notifications"

// and here you can do your conditions based on the current location
Abdelazeem Kuratem
  • 1,324
  • 13
  • 20
  • How does this answer the question? I don't want to get the current route. – iDecode Jun 17 '23 at 15:29
  • I am sorry but that is what I understand from the question title `How to return the existing (current) route from go_router?` – Abdelazeem Kuratem Jul 04 '23 at 12:28
  • That's fine. (1) The title says `How to return ...`, not `How to get ...` (2) StackOverflow also allows users to provide additional information in the body of the post in case the title isn't clear enough. So, spending about 10-15 seconds to read through the body might have given you a better understanding of the question, potentially saving you a few minutes in writing this answer. – iDecode Jul 05 '23 at 18:21