juliansteenbakker / flutter_secure_storage

A Flutter plugin to store data in secure storage
https://pub.dartlang.org/packages/flutter_secure_storage
BSD 3-Clause "New" or "Revised" License
1.14k stars 391 forks source link

The delete all function doesn't work with encryption. #607

Closed Segatti closed 3 months ago

Segatti commented 1 year ago

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.

juliansteenbakker commented 3 months ago

I am closing all older issues. If this issue still exists in the latest version, please let me know.