【发布时间】:2022-07-28 17:08:31
【问题描述】:
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();
}
上述逻辑不起作用,但它与 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();
}
但更好的方法是使用在这种情况下不起作用的 FutureProvider。那么我的代码有什么问题。如何使用具有相同逻辑的 FutureProvider?
【问题讨论】: