How to cancel routing? If user is not premium user, Try to not allow to move to target routing. For example, like below.
redirect: (BuildContext context, GoRouterState state) {
if (AuthState.of(context).isNotPremiumUser) {
// cancel routing
} else {
return null;
}
},
How to do "return false" with go_router?
I did some research but nothing very conclusive.
I found out that If we will throw an exception during redirect, nothing happens
redirect: (BuildContext context, GoRouterState state) {
if (AuthState.of(context).isNotPremiumUser) {
throw Exception('Router declined redirect');
}
return null;
}
You can use the same approach for example showing dialog which asks user if he wants to redirect.
redirect: (BuildContext context, GoRouterState state) async {
if (AuthState.of(context).hasUnfinishedData) {
final allowed = await showDialog(SomeDialog());
if(allowed) return null; // Allows for redirection to the specific page may be returned as well from the dialog
throw Exception('Router declined redirect');
}
return null;
}
Just return the data from the Dialog like this -
Navigator.of(context).pop(true/false);