flutter_secure_storage: `containsKey` always returning true

I’ve just started using flutter_secure_storage in a new project, for some reason containsKey is returning true for every key value I give it.

In the below image (showing a debug session) you can see that containsKey is returning true even though readAll returns an empty map, and I even try deleting the value associated with the key but containsKey still returns true. This is running on an iOS simulator.

Screenshot 2021-11-20 at 20 20 18

Versions:

  • Flutter - 2.5.3
  • Dart - 2.14.4
  • Xcode - 13.0
  • iOS - 15.0
  • flutter_secure_storage - 5.0.2

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 35
  • Comments: 30

Commits related to this issue

Most upvoted comments

I am aware of this issue as well as some other issues with this package. I’m trying to free up some time to work on this but i’m working on more packages that just this one. Like I said in other issues, I encourage people to try and fix it them self and i am very much open for PR’s that could fix this problem.

Seems like this bug has been introduced with the version 5.0.0-beta.5 with the new containsKey method. As a workaround you can go back to the previous implementation using:

(await _flutterSecureStorage.read(key: 'key')) != null

instead of

await _flutterSecureStorage.containsKey(key: 'key')

This is a major bug for such a big package. @juliansteenbakker or @mogol can we timeline on when this can be fixed?

I got the same issue contains key always return true.

I try to testing with multiple version.

I have try to check :

var containsEncryptionKey = await secureStorage.containsKey(key:keyName); // Alway return true. if (!containsEncryptionKey) { var keyGenerate = Hive.generateSecureKey(); await secureStorage.write(key: keyName, value: base64UrlEncode(keyGenerate)); } //var data = await secureStorage.read(key: keyName); // nullable var data = await secureStorage.readAll(); // length ==0 if (data != null && data.length == 0) { // come true. }

flutter_secure_storage - 5.0.0 & 5.0.1 & 5.0.2 Flutter - 2.8.1 Dart - 2.15.1 Xcode - 13.2 iOS - 15.2

Faced the same bug

I’m sure if it had been solved there would be a bug fix release. Last I checked, open source maintainers weren’t paid for their efforts, so the demanding tone is most likely not going to get the results you’re looking for.

Same issue here. Guys, this bug was reported Nov. last year. Has it been solved?

We make an additional check on the value of the key to work around this issue - if the key is reported to be there, we additionally check if it is not null and not empty. This feels wrong but at least people can use our app again… 🙃

It turned out the sharedPreference instance was created multiple times in Android. This was causing lots of problems with not only containsKey, but also trying to read the value using the key. I have fixed these issues, together with containsKey problems on macOS in version 7.0.0. Please check it out and let me know if this fixes the issues 😄

Thanks @juliansteenbakker - that would be awesome. Thank you for giving it a try - can’t ask for much more than that! Throwing some appreciation your way 😃

I understand this is still a problem, and it is not that i don’t want to fix this, but rather i have some other plugins that require more work right now.

@Adam-Langley ofcourse your welcome to sponsor me, but for me its a problem of time and not motivation/money. I have a fulltime job besides working on these plugins and the december months are more busy than others.

As always im open for PR’s with a fix. I’ll also take another look into this tomorrow to see if i can come up with anything else.

As a workaround you can go back to the previous implementation using:

(await _flutterSecureStorage.read(key: 'key')) != null

instead of

await _flutterSecureStorage.containsKey(key: 'key')

At least this works

I have released v6.1.0-beta.1 which probably fixes this issue. Can you please check, and if there is any error comment on this issue?