简体   繁体   English

Flutter - 用户退出应用程序后如何擦除提供程序数据?

[英]Flutter - How to wipe provider data after user signed out from app?

I am using provider for state management in my flutter application.我在我的 flutter 应用程序中使用 state 管理提供程序。 I have implemented provider using Multiprovider .我已经使用Multiprovider实现了提供程序。

I want to wipe provider data when the user signed out from the app I have read out this comment but don't understand it well.当用户从应用程序中退出时,我想擦除提供程序数据我已阅读评论但不太了解它。

If you rebuild your MultiProvider widget with a different key, it reset the state, hence, rebuild the providers.如果您使用不同的密钥重建MultiProvider小部件,它会重置 state,因此,重建提供程序。 That's what is explained there.这就是那里的解释。

Hence, what you need to do in order to achieve it, is something like:因此,为了实现它,您需要做的是:

String userId = myUserId;

void signOut() {
   setState(() => userId = '');
}

Widget build(BuildContext context) {

  return MultiProvider(
    key: ObjectKey(userId),
    providers: // Your providers
    child: SomeSubtree(),
  );
}

Assuming you are using a StatefulWidget , in this case.在这种情况下,假设您使用的是StatefulWidget You can read more about how keys work in Flutter here .您可以在此处阅读有关 Flutter 中密钥如何工作的更多信息。

The idea is to clear the state when the user wants to logOut, mainly by setting the token to null思路是在用户想注销时清除state,主要是通过设置token为null

And also if you stored these user data in the device you should clear them.而且,如果您将这些用户数据存储在设备中,您应该清除它们。 For me I use sharedprefrences.对我来说,我使用共享偏好。

example of my code我的代码示例

  Future<void> logOut() async {
    _token = null;
    notifyListeners();
    final prefs = await SharedPreferences.getInstance();
    prefs.clear();
  }

if you stored other data also set it to null, and be sure to notifyListeners after setting these data to null.如果您存储了其他数据,也将其设置为 null,并确保在将这些数据设置为 null 后通知侦听器。

I suppose that you did the multiprovder correctly, so I only get you the idea to do the LogOut我想你正确地做了 multiprovder,所以我只是让你知道做 LogOut

The problem is when user A logout, and then user B login, the old data of user A still here.问题是当用户 A 注销,然后用户 B 登录时,用户 A 的旧数据仍然存在。

To fix that problem I have 3 solutions:为了解决这个问题,我有 3 个解决方案:

1/. 1/。 In every ChangeNotifierProxyProvider for update prop, have to check state of login status, and return new Provider , otherwise return updated Provider在每个ChangeNotifierProxyProvider for update prop 中,必须检查 state 的登录状态,并返回新的Provider ,否则返回更新的Provider

And on other Provider doesn't depend on login status, we have to manually call the function to clear data.而其他Provider不依赖于登录状态,我们必须手动调用function来清除数据。

2/. 2/。 Another solution is try to rebuild mainApp again when logout.另一种解决方案是在注销时再次尝试重建 mainApp。 Ex: https://stackoverflow.com/a/68572345/1122308例如: https://stackoverflow.com/a/68572345/1122308

And it maybe does not work for some app because structure is different.它可能不适用于某些应用程序,因为结构不同。 If you want it work for your app, please post your app's code.如果您希望它适用于您的应用,请发布您的应用代码。 We can help you.我们能帮你。

3/. 3/。 Restart OS level your app when logout by this package: https://pub.dev/packages/restart_app通过此 package: https://pub.dev/packages/restart_app注销时重新启动操作系统级别的应用程序

It's ok but not good for UX.没关系,但对 UX 不利。 And iOS doesn't work https://github.com/gabrimatic/restart_app/issues/1而 iOS 不起作用https://github.com/gabrimatic/restart_app/issues/1

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

相关问题 如何保持用户登录 Flutter Firebase 应用程序? - How to keep user signed in Flutter Firebase app? Flutter Provider 显示来自错误用户的数据 - Flutter Provider showing data from wrong user Flutter集成测试,如何确保用户退出? (冲洗状态) - Flutter integration test, how to ensure user is signed out? (flushed state) Flutter发布APK擦除数据 - Flutter release apk wipe data Flutter:从应用程序注销后如何清除 Firebase 缓存? - Flutter : How to clear Firebase cache after logging out from app? 如何检查 Firebase 用户最近是否登录了 Flutter 应用程序 - How to check if the Firebase user has recently signed in in a Flutter app 如何从 Flutter Provider 中的 StreamController 中删除数据? - How to delete data from StreamController in Flutter Provider? Flutter - 即使在退出并使用另一个帐户登录后,Google 登录也会使用以前登录的帐户(用户) - Flutter - Google Sign In uses previously signed in account (user) even after signing out and signing in with another account Flutter - 应用启动时从 Firestore 获取数据并保存到 Provider - Flutter - Get data from Firestore and save to Provider when app starts 来自在颤振中热重载后返回数据的提供程序的空值 - Null value from a provider that return data after hot reload in flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM