I use the secure storage package to store data relevant to my app usage.
Recently I created some new features not related to that package and everything works perfectly on development environment. But after deploying the app on the google play store, I get an error when trying to use secure storage.
After clicking on the login button, I try to clean the secure storage using the clear function from the code below.
When I do that, I get an error:
PlatformException(Exception encountered, deleteAll, java.lang.IllegalArgumentException: bad base-64 at
u4.e.c(Unknown Source:37) at
u4.e.b(Unknown Source:2) at
u4.e.a(Unknown Source:6) at
androidx.security.crypto.a.c(Unknown Source:3) at
androidx.security.crypto.a.getAll(Unknown Source:49) at
androidx.security.crypto.a$b.a(Unknown Source:11) at
androidx.security.crypto.a$b.apply(Unknown Source:0) at
s6.a.e(Unknown Source:23) at s6.e$b.run(Unknown Source:135) at
android.os.Handler.handleCallback(Handler.java:942) at
android.os.Handler.dispatchMessage(Handler.java:99) at
android.os.Looper.loopOnce(Looper.java:240) at
android.os.Looper.loop(Looper.java:351) at
android.os.HandlerThread.run(HandlerThread.java:67) , null)
On the moment that the error triggers the storage has 2 values in it that would be cleaned. An email and a JWT token.
So far the error only happens on my own smartphone (OnePlus 8 Pro + Oxygen OS 13) and on this new version installed from the play store. Other people haven't been able to reproduce the error I get. Also, the code that now gives me this error exists, implemented exactly as in this version, on the app since the first version (I already updated the app a few times) and i never had that error before.
My secure storage implementation
class SecureStorageService {
static final SecureStorageService _instance =
SecureStorageService._internal();
late FlutterSecureStorage flutterSecureStorage;
SecureStorageService._internal() {
flutterSecureStorage = const FlutterSecureStorage(
aOptions: AndroidOptions(
encryptedSharedPreferences: true,
));
}
static SecureStorageService get getInstance => _instance;
Future<bool> set({required String key, required String value}) async {
await flutterSecureStorage.write(
key: key,
value: value,
);
return true;
}
Future<String?> get({required String key}) async {
return flutterSecureStorage.read(key: key);
}
Future<bool> clear() async {
await flutterSecureStorage.deleteAll(
aOptions: const AndroidOptions(encryptedSharedPreferences: true));
return true;
}
}
Things I already tried based on some research:
- adding aOptions: const AndroidOptions(encryptedSharedPreferences: true)
- update flutter to its latest version 3.3.10
- update storage package to its latest version 7.0.1
- tried to play with android:allowBackup and android:fullBackupContent
Another thing I noticed is that if I clear the app data on the android, the error does not happen. But if I uninstall and install the app again, the error comes back until I clear the app data again
Considering the fact that cleaning the app's data from android "solves the problem", I tried to do that programmatically with things like:
await DefaultCacheManager().emptyCache();
Future<void> _deleteCacheDir() async {
final cacheDir = await getTemporaryDirectory();
if (cacheDir.existsSync()) {
cacheDir.deleteSync(recursive: true);
}
}
Future<void> _deleteAppDir() async {
final appDir = await getApplicationSupportDirectory();
if (appDir.existsSync()) {
appDir.deleteSync(recursive: true);
}
}
But it didn't work. I think that what I'm doing through code is probably not equivalent to what android does.