flutter_secure_storage
flutter_secure_storage copied to clipboard
Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)'
PlatformException(Exception encountered, read, java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference at com.it_nomads.fluttersecurestorage.FlutterSecureStoragePlugin$b.run(SourceFile:24) at android.os.Handler.handleCallback(Handler.java:938) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:246) at android.os.HandlerThread.run(HandlerThread.java:67) , null)
This issue was supposed to be fixed with #332 , but is still hapenning in 5.0.2
Only Android devices are affected by this issue.
I encountered a similar error. What I did was disabling backup in my AndroidManifest.xml:
<application
...
android:allowBackup="false"
android:fullBackupContent="false">
Hi everyone! I've been having this issue as well. As @tahitiangabriel say, it's mentioned in #332 but also in #53. This last one is the most similar to my error, as the people updating the app are getting this error that leaves the APP totally broken.
It seems that 5.0.2 is not resolving these issues as it appears that there are people reopening and commenting on these issues. In my case, not everybody gets the error, it's like 50/50, don't know if it may be linked to SO versions, iOS, Android or what...
I will try to add a button to "Clear cache" or automatically deleteKeys on error to see if it works but definitely is not the best option... Let's see if we find a solution soon 😢
@dustincatap Could you explain your solution and why should work? Don't know why backup could be causing trouble in here. Thanks!
E/FlutterSecureStoragePl( 6611): EncryptedSharedPreferences initialization failed
E/FlutterSecureStoragePl( 6611): com.google.crypto.tink.shaded.protobuf.InvalidProtocolBufferException: Protocol message contained an invalid tag (zero).
E/FlutterSecureStoragePl( 6611): at com.google.crypto.tink.shaded.protobuf.y.K(GeneratedMessageLite.java:10)
E/FlutterSecureStoragePl( 6611): at com.google.crypto.tink.shaded.protobuf.y.H(GeneratedMessageLite.java:2)
E/FlutterSecureStoragePl( 6611): at p4.c0.X(Keyset.java:1)
E/FlutterSecureStoragePl( 6611): at m4.d.a(SharedPrefKeysetReader.java:1)
E/FlutterSecureStoragePl( 6611): at i4.b.a(CleartextKeysetHandle.java:1)
E/FlutterSecureStoragePl( 6611): at m4.a$b.e(AndroidKeysetManager.java:4)
E/FlutterSecureStoragePl( 6611): at m4.a$b.f(AndroidKeysetManager.java:1)
E/FlutterSecureStoragePl( 6611): at m4.a$b.d(AndroidKeysetManager.java:3)
E/FlutterSecureStoragePl( 6611): at z0.a.b(EncryptedSharedPreferences.java:8)
E/FlutterSecureStoragePl( 6611): at z0.a.a(EncryptedSharedPreferences.java:1)
E/FlutterSecureStoragePl( 6611): at g7.d.s(FlutterSecureStoragePlugin.java:7)
E/FlutterSecureStoragePl( 6611): at g7.d.p(FlutterSecureStoragePlugin.java:8)
E/FlutterSecureStoragePl( 6611): at g7.d.b(FlutterSecureStoragePlugin.java:1)
E/FlutterSecureStoragePl( 6611): at g7.d$b.run(FlutterSecureStoragePlugin.java:23)
E/FlutterSecureStoragePl( 6611): at android.os.Handler.handleCallback(Handler.java:938)
E/FlutterSecureStoragePl( 6611): at android.os.Handler.dispatchMessage(Handler.java:99)
E/FlutterSecureStoragePl( 6611): at android.os.Looper.loopOnce(Looper.java:201)
E/FlutterSecureStoragePl( 6611): at android.os.Looper.loop(Looper.java:288)
E/FlutterSecureStoragePl( 6611): at android.os.HandlerThread.run(HandlerThread.java:67)
E/flutter ( 6611): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: PlatformException(Exception encountered, read, java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.content.SharedPreferences.contains(java.lang.String)' on a null object reference
E/flutter ( 6611): at g7.d$b.run(FlutterSecureStoragePlugin.java:24)
E/flutter ( 6611): at android.os.Handler.handleCallback(Handler.java:938)
E/flutter ( 6611): at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter ( 6611): at android.os.Looper.loopOnce(Looper.java:201)
E/flutter ( 6611): at android.os.Looper.loop(Looper.java:288)
E/flutter ( 6611): at android.os.HandlerThread.run(HandlerThread.java:67)
E/flutter ( 6611): , null)
E/flutter ( 6611): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607)
E/flutter ( 6611): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:177)
E/flutter ( 6611): <asynchronous suspension>
E/flutter ( 6611): #2 main (package:ddv_loyalty/main.dart:37)
E/flutter ( 6611): <asynchronous suspension>
E/flutter ( 6611):
version 5.0.2 still error when build app release mode
You are getting
E/FlutterSecureStoragePl( 6611): EncryptedSharedPreferences initialization failed
Initialization failed does not seem to be the same case that we are exposing here. Are you sure that you've reloaded properly your app after installing the library?
i happen when i build to physical device in Release mode
i happen when i build to physical device in Release mode
+1
I am also facing similar issue, issue: flutter PlatformException(Exception encountered, write, java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
Any further update on this issue?
I had this same error appear to me in a very specific scenario. I hope this report will bring some light in the issue.
My app was working fine during development until I tried to uninstall and install it again to be sure that everything was going to work properly. Then the error occurred. I was using the --release flag on build.
It seems to only happen on Android 11 (tried on two Xiaomi Mi 10T and on Samsung A31) on my code. I tried to reproduce this scenario on other Android versions (6, 9 and 12) but everything is running smoothly, even after reinstalling the app.
Firebase Crashlytics reports the error happening when I call .containsKey or .read when trying to read previously stored user data to determine whether to show Login Page or the main route.
@juliansteenbakker can someone please take a closer look at this? This is affecting a ton of users and really hard to debug.
I downgraded to 4.2.1 and disabling encryptedSharedPreferences fixed this for me.
I have the same problem. Occurs continuously in release mode. It's a complicated environment to debug, so it's hard to deal with... 😢
@lucianosb
I solved this problem.
If your versions upper 5.0.0 & it was build to release mode, try to this code.
storage.read(key: 'key')
to
storage.read(key: 'key', aOption: AndroidOptions(encryptedSharedPreferences: true,)
I've tried https://github.com/mogol/flutter_secure_storage/issues/380#issuecomment-1236577186 and also an update to 6.0.0 but the main issue is still pressent.
@mogol For now this is blocker for our app :(
@mogol Same thing. allowBackup is false, fullBackupContent is false, aOption: AndroidOptions(encryptedSharedPreferences: true,) is on every call.
The exception is happening on devices using Android 10 and above.
I'm mostly getting it when accessing EncryptedSharedPreferences during background work (using workmanager).
This issue is completely preventing users from using the app. Could share some thoughts on what the issue could be ? It clearly has to do with getting the app's context.
One of my users unblocked himself by Force stopping the app then clearing data & cache. Didn't work for others but I have no way of knowing if they proceeded correctly or if this is really what unlocked that one user.
So it seems forcing a deletion solves the problem. Any way of doing this reliably @mogol ?
I will check once more. But my previous attempts to reproduce and fix reliably were not very successful. So no promises 🤷♂️
I have put resetOnError to true on all accesses, and the error still occurs.
Alright, seem I'm getting contacted at least once a day by a user with this issue, I'm going to have to drop this package.
@mogol Please put a warning for the use of EncryptedSharedPreferences with this package. It is not stable and such a warning would have saved me a whole lot of trouble. Best of luck man.
I solved this problem with
static AndroidOptions _getAndroidOptions() => const AndroidOptions( keyCipherAlgorithm: KeyCipherAlgorithm.RSA_ECB_OAEPwithSHA_256andMGF1Padding, storageCipherAlgorithm: StorageCipherAlgorithm.AES_GCM_NoPadding, );
Any update in that topic? We are having few crashes on the daily basis. The error message looks similar:
java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
We are using encryptedSharedPreferences option enabled.
Any update?
Our crash analysis tool shows us, that this situation mostly occurs on Android 6, especially on Huawei P8 Lite device. Maybe that will help?
Hello, any update? I'm still getting this error.
How to reproduce:
- Install apps from play store.
- Uninstall apps
- Install apps from play store again.
- Open apps, and when apps call the "read" method will catch this error.
I think this is related to this https://developer.apple.com/forums/thread/36442 and https://stackoverflow.com/questions/4747404/delete-keychain-items-when-an-app-is-uninstalled
We are having this issue on the Android platform only - it's not related to iOS. As far as I know, the Android removes the data from the encrypted preferences once app is uninstalled.
We are having this issue on the Android platform only - it's not related to iOS. As far as I know, the Android removes the data from the encrypted preferences once app is uninstalled.
Not sure. What if the manifest has android:allowBackup="true"?
I'm wondering, do using both FlutterSecureStorage and SharedPreferences in the same app can cause this kind of errors ?
@jeff-odopass That's a great question. We had the same combination in our app... Do you have some additional info as to why you suspect that that combination could cause problems?
@jfahrenkrug I have no clue and my app also have both plugins working simultaneously.
What is strange is that both plugins triggers the same error, which makes me think it may not be plugin-specific.
Both plugins may also not have been designed to work together so I will give it a try by replacing SharedPreferences by FlutterSecureStorage.
Keeping you updated with that