Flutter:在安装/卸载过程中如何删除FlutterSecureStorage项目 [英] Flutter: how do I delete FlutterSecureStorage items during install/uninstall

查看:241
本文介绍了Flutter:在安装/卸载过程中如何删除FlutterSecureStorage项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FlutterSecureStorage存储某些项目,即用于访问服务器端资源的API令牌.但是,我遇到了一个奇怪的问题.我不得不删除数据库(因为我仍处于测试模式,这种情况现在经常发生),该数据库也删除了所有令牌.但是,当应用程序尝试连接时,会出现错误.

I am using FlutterSecureStorage to store certain items, namely the API tokens to access server side resources. However, I've run into a weird issue. I had to delete the database (as I'm still in testing mode, this happens quite frequently for now), which deleted all the tokens as well. But when the app tries to connect it gets an error.

在Android上,这没什么大不了的.我只是卸载并重新安装该应用程序,它将下载一个新的令牌.

On Android, this is not a big deal. I just uninstall and reinstall the app and it will download a fresh token.

在iOS上存在问题.由于FlutterSecureStorage将所有信息存储在钥匙串中,因此即使已卸载应用程序,该数据也不会被删除.因此,在我重新安装它之后,它仍然会从存储中获取令牌,并且我无法刷新令牌.

On iOS, there's a problem. Because FlutterSecureStorage stores any info in the keychain, the data doesn't get deleted even if the app is uninstalled. So after i reinstall it, it still gets the token from storage, and I can't refresh the token.

我的问题是:在Flutter中安装或卸载期间,是否可以通过某种方式运行代码来删除所有存储项?

My question is: Is there some way to run code to remove all Storage items during install or uninstall in Flutter?

推荐答案

在iOS上,您可以使用 NSUserDefaults ,它会在应用卸载后被删除.这样,您可以检查应用程序在卸载后是否首次启动.使用 shared_preferences Flutter插件来使用 NSUserDefaults .

On iOS you can make use of NSUserDefaults, which does get deleted on an app uninstall. That way you can perform a check whether the app is starting for the first time after an uninstall. Use the shared_preferences Flutter plugin to make use of NSUserDefaults.

之前已在其他平台的StackOverflow上讨论了这种方法,请参见卸载应用程序后删除钥匙串项(例如,其他语言的版本).

This approach has been discussed on StackOverflow for other platforms before, see Delete keychain items when an app is uninstalled for examples in other languages.

对于Flutter,此示例将变为:

For Flutter this example would become:

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:shared_preferences/shared_preferences.dart';

// ...

final prefs = await SharedPreferences.getInstance();

if (prefs.getBool('first_run') ?? true) {
  FlutterSecureStorage storage = FlutterSecureStorage();

  await storage.deleteAll();

  prefs.setBool('first_run', false);
}

这篇关于Flutter:在安装/卸载过程中如何删除FlutterSecureStorage项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆