I have a simple FutureProvider
that holds a bool value:
@riverpod
Future<bool> isAuthenticated(IsAuthenticatedRef ref) async {
// Some async logic here
return await getAuthState();
}
I use auto_route
package which provides the reevaluateListenable
param in the router config to allow re-evaluate the route stack and re-call route guards when some value changes:
MaterialApp.router(
routerConfig: _appRouter.config(
reevaluateListenable: authProvider,
),
);
I need to perform re-evaluation when authentication state changes, the problem is that reevaluateListenable
requires a Listenable
, but FutureProvider
does not implement it.
I’ve tried implementing authChangeNotifierProvider
:
class AuthChangeNotifier extends ChangeNotifier {
bool isAuthenticated = false;
final AutoDisposeChangeNotifierProviderRef <AuthChangeNotifier> ref;
AuthChangeNotifier({required this.ref}) {
ref.listen(tokenPairProvider, (_, newValue) {
isAuthenticated = newValue.when(
data: (data) => data.isAuthenticated,
error: (_, __) => false,
loading: () => false);
notifyListeners();
});
}
}
final authChangeNotifierProvider = ChangeNotifierProvider.autoDispose<AuthChangeNotifier>((ref) {
return AuthChangeNotifier(ref: ref);
});
This way I can pass listenable to the router config, but then I have no idea how to access the actual value inside the route guard. What would be the best way to achieve this?