auto_route_library
auto_route_library copied to clipboard
Race condition with guards when using router.pop() while reevaluating
Hey there,
Just to give context to the issue I found: I am utilizing reevaluateListenable
to automatically update my guards when any of the user's information changes with a dialog that is behind a guard. However, I have noticed a race condition when you trigger a reevaluation and then immediately call `context.router.pop(); to close the dialog: the dialog closes and then opens up again rapidly.
This happens because:
- The user information is updated
-
reevaluateListenable
is triggered because of this (which starts the guard reevaluation) - I manually call
context.router.pop()
to close the dialog - The dialog is closed
- The guard completes it's reevaluation, and then calls
resolver.next()
- Since
resolver.next();
was called, the dialog opens again
I was wondering if there was a way to have context.router.pop()
wait until current guards have been reevaluated before changing routes? Or perhaps abort the guard reevaluation to prevent it from calling resolver.next()
if the route being reevaluated isn't in the stack anymore.
Happy to put together a MRE, but wanted to see if anyone had some ideas before I do it.
Thank you
Hey @jointhejourney did you try doing something with NavigationResolver.isReevaluating
?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions
I have this issue too
Here's my Guard
class AppGuard extends AutoRouteGuard {
@override
void onNavigation(NavigationResolver resolver, StackRouter router) {
final BuildContext? context = router.navigatorKey.currentContext;
optionOf(context).fold(
() => resolver.next(),
(context) {
final bool isAppGuarded =
context.read<SettingsCubit>().state.isAppGuarded;
resolver.next();
log('Does App Protected Now? $isAppGuarded');
if (isAppGuarded && router.currentUrl == '/' ) {
router.navigate(AppGuardRoute());
}
},
);
}
}
Hey @jointhejourney did you try doing something with
NavigationResolver.isReevaluating
?
Hey there,
Just to give context to the issue I found: I am utilizing
reevaluateListenable
to automatically update my guards when any of the user's information changes with a dialog that is behind a guard. However, I have noticed a race condition when you trigger a reevaluation and then immediately call `context.router.pop(); to close the dialog: the dialog closes and then opens up again rapidly.This happens because:
- The user information is updated
reevaluateListenable
is triggered because of this (which starts the guard reevaluation)- I manually call
context.router.pop()
to close the dialog- The dialog is closed
- The guard completes it's reevaluation, and then calls
resolver.next()
- Since
resolver.next();
was called, the dialog opens againI was wondering if there was a way to have
context.router.pop()
wait until current guards have been reevaluated before changing routes? Or perhaps abort the guard reevaluation to prevent it from callingresolver.next()
if the route being reevaluated isn't in the stack anymore.Happy to put together a MRE, but wanted to see if anyone had some ideas before I do it.
Thank you
Ho did you fix this, I am facing the same issue along with a lot of other issues with reevaluate and guards
Honestly, I just did a hacky workaround. I'm waiting 10 miliseconds which is enough time for the guards to re-evaluate. I haven't been able to get back to this and see if there's a way to prevent the race condition from happening.
Future.delayed(const Duration(milliseconds: 10), () {
context.router.pop();
});
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions