I wrote a simple extension to get the current route. So, final route = Navigator.of(context).currentRoute() where
extension NavigatorStateX on NavigatorState {
Route<dynamic> currentRoute() {
return ModalRoute.of(context);
}
}
It works fine for a local navigator, but when I use it on a root navigator, ModalRoute.of(context) returns null. E.g.,
Navigator.of(context, rootNavigator: true).pushReplacementNamed('/home');
final route = Navigator.of(context, rootNavigator: true).currentRoute();
// ^^^^^ route is null
Not sure why it works for a local navigator but not the root?
EDIT: I wrote a simple workaround function that fetches the current Route and works for root Navs:
Route<dynamic>? currentRoute() {
Route<dynamic>? result;
// Workaround (never pops, predicate always returns true)
popUntil((route) {
result = route;
return true;
});
return result;
}
Would like something clean though.