Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check which the current Route is?

Tags:

flutter

dart

I want to navigate to different Routes using a Drawer, though I do not want to open a new instance of a Route each time I tap on it if I am already on that Route, rather I would prefer that in this case a new Route is not opened. This is my code so far:

Widget build(BuildContext context){     return new Drawer(       child:           new ListView(             children: <Widget>[               new ListTile(                 title: new Text("NewRoute"),                 onTap: () {                     Navigator.of(context).pop;                     Navigator.of(context).pushNamed('/NewRoute');                 }               )            )      ) } 

I want to use a conditional statement to check whether we are on a certain route. I know there is a way to check which Route we are on currently with the isCurrent of the Route class

https://docs.flutter.io/flutter/widgets/Route/isCurrent.html

though I am not sure how to implement it.

Thank you in advance!

like image 765
Marko Avatar asked Jun 12 '18 12:06

Marko


People also ask

What is current route in Angular?

There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service.

How do I find out my current route Vue?

We can use this. $router. currentRoute. path property in a vue router component to get the current path.


1 Answers

Navigator doesn't expose the current route.

What you can do instead is use Navigator.popUntil(callback) as popUtil pass to the callback the current Route, which includes it's name and stuff.

final newRouteName = "/NewRoute"; bool isNewRouteSameAsCurrent = false;  Navigator.popUntil(context, (route) {   if (route.settings.name == newRouteName) {     isNewRouteSameAsCurrent = true;   }   return true; });  if (!isNewRouteSameAsCurrent) {   Navigator.pushNamed(context, newRouteName); } 
like image 94
Rémi Rousselet Avatar answered Oct 07 '22 14:10

Rémi Rousselet