Use didPopRoute
to handle back button:
@override
Future<bool> didPopRoute() {
bool shouldAllowBack = true; // Update condition if required
if (shouldAllowBack) {
return super.didPopRoute(); // allow back
} else {
// Add logic here if required
return Future.value(true); // back not allowed
}
}
& It works fine with GoRouter
.
Exact code:
@override
Future<bool> didPopRoute() {
return Future.value(true);
}
Official Documentaion:
Flutter > widgets.dart > WidgetsBindingObserver > didPopRoute method