'How can I react to a riverpod FutureProvider by using ref.listen?
final nextRouteProvider = FutureProvider<String>((ref) async {
await Future.delayed(const Duration(seconds: 3));
bool isAppFreshInstall = StorageManager.instance.isAppFreshInstall();
if (isAppFreshInstall) {
return AppRouter.onBoardingPath;
} else {
return AppRouter.loginPath;
}
});
@override
Widget build(BuildContext context, WidgetRef ref) {
ref.listen<Future<String>>(nextRouteProvider.future, (_, Future<String> path) async {
context.go(await path);
});
return SplashScreen();
}
The above logic is not working but it works well with StateNotifierProvider.
class RootViewNotifier extends StateNotifier<String> {
RootViewNotifier() : super('/') {
decideRootView();
}
void decideRootView() async {
await Future.delayed(const Duration(seconds: 3));
var storageManager = StorageManager.instance;
if (storageManager.isAppFreshInstall()) {
state = AppRouter.onBoardingPath;
} else {
state = AppRouter.loginPath;
}
}
}
final rootViewNotifierProvider =
StateNotifierProvider<RootViewNotifier, String>(() => RootViewNotifier());
@override
Widget build(BuildContext context, WidgetRef ref) {
ref.listen<String>(rootViewNotifierProvider, (, String path) {
context.go(path);
});
return SplashScreen();
}
But the better way is to use FutureProvider which is not working in this case. So what's wrong with my code. How can I use FutureProvider with the same logic?
Solution 1:[1]
Listen to nextRouteProvider instead of nextRouteProvider.future
Like so:
ref.listen(
nextRouteProvider,
(AsyncValue<String>? _, AsyncValue<String> next) {
context.go(next.asData!.value);
},
);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
