flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

The delete all function doesn't work with encryption.

Open Segatti opened this issue 2 years ago • 0 comments

my class for storage

class StorageHelper {
  late FlutterSecureStorage _storage;

  StorageHelper() {
    _storage = FlutterSecureStorage(
      aOptions: _getAndroidOptions(),
      iOptions: _getIOSOptions(),
    );
  }

  AndroidOptions _getAndroidOptions() => const AndroidOptions(
        encryptedSharedPreferences: true,
      );

  IOSOptions _getIOSOptions() => const IOSOptions(
        accessibility: KeychainAccessibility.first_unlock,
      );

  Future<void> setData(String key, String? value) async {
    try {
      await _storage.write(
        key: key,
        value: value,
        aOptions: _getAndroidOptions(),
        iOptions: _getIOSOptions(),
      );
    } catch (_, stackTrace) {
      await Sentry.captureException(
        "Storage - setData",
        stackTrace: stackTrace,
        withScope: (scope) => scope.setExtra(key, value),
      );
      throw (StorageError("Error: Falha ao escrever dados"));
    }
  }

  Future<String?> getData(String key) async {
    try {
      return await _storage.read(
        key: key,
        aOptions: _getAndroidOptions(),
        iOptions: _getIOSOptions(),
      );
    } catch (_, stackTrace) {
      await Sentry.captureException(
        "Storage - getData",
        stackTrace: stackTrace,
        withScope: (scope) => scope.setExtra(key, "error"),
      );
      throw (StorageError("Error: Falha ao escrever dados"));
    }
  }

  Future<void> removeData(String key) async {
    try {
      await _storage.delete(
        key: key,
        aOptions: _getAndroidOptions(),
        iOptions: _getIOSOptions(),
      );
    } catch (_, stackTrace) {
      await Sentry.captureException(
        "Storage - removeData",
        stackTrace: stackTrace,
        withScope: (scope) => scope.setExtra(key, "error"),
      );
      throw (StorageError("Error: Falha ao remover dados"));
    }
  }
...

  Future<void> clearStorage() async {
    try {
      _storage.deleteAll(
        aOptions: _getAndroidOptions(),
        iOptions: _getIOSOptions(),
      );
    } catch (_, stackTrace) {
      await Sentry.captureException(
        "Storage - clearStorage",
        stackTrace: stackTrace,
      );
      throw (StorageError("Error: Falha ao limpar dados"));
    }
  }
}

In the first execution after installing the app, the storage generates a bug that cannot execute deleteAll, nothing appears in the logs, it just keeps loading infinitely, as if it could not execute. When I exit the app and clear the app's cache, it goes back to working normally with all the features.

Segatti avatar Sep 06 '23 14:09 Segatti