卸载应用程序时清除共享首选项?

时间:2020-09-22 11:59:43

标签: flutter dart sharedpreferences

我正在使用共享首选项来存储当前用户的名称,头像和该用户是否已登录的布尔值。卸载应用程序似乎在重新安装时导致问题。它似乎与shared preferences有关,因为当我注销并重新登录时,问题就消失了。我怀疑共享首选项文件由于某种原因被损坏,这会引发整个应用程序的问题。

那么有什么方法可以在卸载后清除或删除共享的首选项文件吗?

如果有帮助,这是注销代码;

Future<void> logOutSocial() async {
    try {
      socState();
      await socialLogin.logOutFacebook();
      await socialLogin.logOutGoogle();
      await socialLogin.logOutTwitter();

      currentname = "Anonymous";
      currentavatar = "https://example.com/default.jpg";
      currentlogged = false;
      currentuserid = "0";

      await savePreferences(
        currentname: "Anonymous",
        currentavatar: "https://example.com/default.jpg",
        currentlogged: false,
      );

      notifyListeners();
    } catch (e) {
      print(e);
    }
  }

2 个答案:

答案 0 :(得分:1)

Android

有两种选择:

  1. 禁用自动备份

从API级别23(Android 6)开始,默认情况下“自动备份”设置为true。您需要编辑AndroidManifest.xml文件并设置android:allowBackup的布尔值。您的情况应该类似于:

 <manifest ...>
        ...
        <application android:allowBackup = "false">
        </application>
    </manifest>

More info about AutoBackup

  1. 声明要存储的内容

the documentation中对所有内容的描述都非常清楚。

iOS

检出this

答案 1 :(得分:0)

Owczar的建议是我在开发过程中更改了程序包名称,这似乎是我的问题。但是我决定将Hive用作替代“共享首选项”的插槽,由于不再需要“共享首选项”文件,因此解决了该问题。

相关问题