简体   繁体   English

在Flutter中使用Riverpod实现session用户数据管理

[英]Using Riverpod in Flutter to implement session user data management

I am using Riverpod in Flutter to handle Firebase authentication, and it seems to be working fine.我在 Flutter 中使用 Riverpod 来处理 Firebase 身份验证,它似乎工作正常。 Additionally, I'd like to use Riverpod to handle core user data (ie "session" data).此外,我想使用 Riverpod 来处理核心用户数据(即“会话”数据)。 The problem is that if a user logs out and another logs in, the user session data is not fetched/refreshed for that new user.问题是,如果一个用户注销并且另一个用户登录,则不会为该新用户获取/刷新用户 session 数据。

Authentication (and the semi-working session data handling) is handled the following way:身份验证(以及半工作 session 数据处理)按以下方式处理:

return authState.when(
    data: (authData) {
      if (authData != null) {

        // Get user data here:
        final userState = ref.watch(appUserProvider);

        return userState.when(
            data: (appUser) {
              if (appUser.isFirstRun) {
                return const OnboardingPage();
              } else {
                return const AppRoot();
              }
            },
            loading: () => const LoadingScreen(),
            error: (e, stackTrace) => ErrorDisplay(e, stackTrace));
      }
      return const LoginPage();
    },
    loading: () => const LoadingScreen(),
    error: (e, stackTrace) => ErrorScreen(e, stackTrace));

As seen above, I'd like the appUserProvider to be provided once the auth state has been provided, but am having trouble getting this nested approach to work properly.如上所示,我希望在提供 auth state 后提供 appUserProvider,但我无法让这种嵌套方法正常工作。 When one user logs out and another logs in, the data is not automatically refreshed without an app restart.当一个用户注销而另一个用户登录时,如果不重新启动应用程序,数据不会自动刷新。 Trying to refresh the appUserProvider explicitly (using ref.read()) does not work either.尝试显式刷新 appUserProvider(使用 ref.read())也不起作用。

The appUserProvider looks like this: appUserProvider 看起来像这样:

   final appUserProvider = StateNotifierProvider<AppUserNotifier, AsyncValue<AppUser>>((ref) {
      return AppUserNotifier(ref);
    });
        
    class AppUserNotifier extends StateNotifier<AsyncValue<AppUser>> {
      final StateNotifierProviderRef ref;
        
      late final UserService service;
        
      AppUserNotifier(this.ref) : super(AsyncValue.data(AppUser())) {
        service = ref.watch(userService);
        get();
    } 
// get method omitted

How can I get this to work properly?我怎样才能让它正常工作? Is it even a good approach?这甚至是一个好方法吗?

Thankful for any input here!感谢您在这里的任何输入!

You can use this for your StateNotifierProvider:您可以将它用于您的 StateNotifierProvider:

final appUserProvider = StateNotifierProvider.autoDispose<AppUserNotifier, AsyncValue<AppUser>>((ref) {
      return AppUserNotifier(ref);
    });

Then the provider will be disposed of as soon as it is no longer used.然后,一旦不再使用该提供程序,它将立即被处理掉。 When a new user logs in, the provider is initialized again with the correct data.当新用户登录时,将使用正确的数据再次初始化提供程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM