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



我使用共享首选项来存储当前用户的姓名、头像和用户是否登录的布尔值。卸载应用程序似乎会在重新安装时导致问题。它似乎与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);
}
}

Android

有两种选择:

  1. 禁用自动备份

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

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

有关自动备份的更多信息

  1. 声明要存储的内容

文档中对所有内容的描述都非常清楚。

iOS

看看这个。

Owczar建议我在开发过程中更改包名,这似乎是我的问题。但我决定使用Hive作为共享首选项的替代位置,这解决了问题,因为共享首选项文件不再必要。

最新更新