Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: how do I delete FlutterSecureStorage items during install/uninstall

Tags:

ios

flutter

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.

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

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.

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

like image 418
starman1979 Avatar asked Sep 14 '19 06:09

starman1979


2 Answers

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.

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.

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);
}
like image 179
Tim Klingeleers Avatar answered Nov 20 '22 01:11

Tim Klingeleers


The other answer is totally correct, but keep in mind that if you launch this over production apps and don't have ever used prefs.getBool('first_run'), this is going to return null, and you are going to delete all items in storage which can be more than the desired one.

Suggestion: Keep the track of items that you really want to remove if you detect the "install" event.

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

// ...
final keysToEliminate = [
  // Store all keys you would like to eliminate in case you are running a 
  // feature for the first time
];
final prefs = await SharedPreferences.getInstance();

if (prefs.getBool('first_run') ?? true) {
   FlutterSecureStorage storage = FlutterSecureStorage();
   await Future.wait(keysToEliminate.map((key) => storage.delete(key: key)));

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

If you truly want to remove all the keys, use this but be careful because "install" and "update" events are impossible to differentiate if you haven't use first_run flag before.

like image 1
Yansaro Rodriguez Paez Avatar answered Nov 20 '22 01:11

Yansaro Rodriguez Paez