flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

Flutter secure storage not storing data after I closed app IOS

Open stefanoHTB opened this issue 2 years ago • 13 comments
trafficstars

I am having problems storing a token with flutter secure storage!

When I close the app or hot-restart the data gets lost or returns null

Any Suggestions?

-------------------------------------------------------------------- code

class Storage { final storage = const FlutterSecureStorage();

final String _token = 'token';

Future setToken(String token) async { const options = IOSOptions(accessibility: KeychainAccessibility.first_unlock); await storage.write(key: _token, value: token, iOptions: options); }

Future<String?> getToken() async { const options = IOSOptions(accessibility: KeychainAccessibility.first_unlock); return await storage.read(key: _token, iOptions: options); } }

stefanoHTB avatar Mar 05 '23 01:03 stefanoHTB

This will help https://www.youtube.com/watch?v=JogvnnaJ8TI

BraveEvidence avatar Mar 15 '23 11:03 BraveEvidence

I'm noticing this in a specific scenario where I log out and log in of my app. So basically, we wipe the stored data, and then store a new token when logging in. After restarting/hot restart; same behavior as @stefanoHTB described. If I login and store a token after a clean start; there is no issue upon restart.

In my case, this problem occurs on both Android and iOS

jannisnikoy avatar Mar 21 '23 18:03 jannisnikoy

Hello, if you do getToken a second time, will the value still be null? I encountered a strange behavior that sometimes when I start the application, the value is returned null first time. However, it returns value after a certain amount of time when I try to do getToken again. And only on iOS.

StigUK avatar Mar 23 '23 12:03 StigUK

Duplicates #524 ?

florentmorin avatar Apr 01 '23 16:04 florentmorin

Having the same problem.

Did anyone found a solution?

phillsch avatar Jul 06 '23 11:07 phillsch

Having this same problem on MacOS. flutter secure storage version 9.0

codelovercc avatar Dec 31 '23 16:12 codelovercc

@codelovercc Same issue on macOS, did not test iOS yet.

  • flutter_secure_storage 9.0.0
  • macOS 14.3
  • Flutter 3.19.1
  • Dart 3.3.0
  • XCode 15.2

How are you all fixing this? I see no solution, at least not with an understandable explanation.

I'm beginning to think this PKG is not working well enough, pity. I will circle back to the issue later. I still need to test it on all platforms.

Edit:

  • Tested WEB release build, that works OK.

rydmike avatar Feb 21 '24 23:02 rydmike

@rydmike Enable keychain this will help you. I've tested on macOS 12.7.1 (21G920) and Android Simulator API 34, it works.

codelovercc avatar Feb 22 '24 16:02 codelovercc

Thanks @codelovercc for the suggestion. I have tried enabling keychain on both iOS (17.0.1 (21A342) and macOS Version 14.3 (23D56) in all modes debug/profile/release. No luck.

I also tried with synchronizable: set to both to true and false.

    mOptions: MacOsOptions(      
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),
    iOptions: IOSOptions(      
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),

Made no difference, if it was true or false, or if that accessibility: KeychainAccessibility.first_unlock was there or not.

The flutter_secure_storage just always reads blank data when it starts up. While running the app it sets and gets data fine in it. But close the app and start it again, and it is gone.

rydmike avatar Feb 23 '24 03:02 rydmike

@rydmike Can you try it in a new flutter project?

codelovercc avatar Feb 23 '24 07:02 codelovercc

Same problem occurring on Windows.

hkh777uk avatar Feb 24 '24 18:02 hkh777uk

@codelovercc I have tried, and also tried recreating the runner on macos, by deleting it and making a new one.

I have also tried this part with the AppIdentifierPrefix

Screenshot 2024-03-10 at 17 54 45

mentioned here: https://github.com/mogol/flutter_secure_storage Which is not mentioned in the published readme here btw https://pub.dev/packages/flutter_secure_storage

All in all it did not help either.

I am a bit unsure of it should be the same as "Bundle identifier" in xcode, that is seems to put into Keychain Sharing under Keychain Groups. Xcode did that edit. Then I don't know if that should then also be used in MacOsOptions as groupId. Docs are very unclear on this, but I have tried pretty much every combo I can think of and nothing worked.

This package is a bit frustrating to use, since it seem to be close to impossible to get it to work on all platforms. Any help is of course appreciated, but I may need to come up with some alternative soonish, not urgent yet. Still not sure if there is any other good and reputable option available.

rydmike avatar Mar 10 '24 16:03 rydmike

Got it working on macOS. Here is how.

When you are using key-chain-access-groups and Xcode makes one based on your app bundle name e.g. com.domain.app you do need to have this in all your entitlements debug/profile/release as well:

	<key>keychain-access-groups</key>
	<array>
		<string>$(AppIdentifierPrefix)com.domain.app</string>
	</array>

And I did, but it was still not working.

However, do not use any groupID in your FlutterSecureStorage.mOptions:

    mOptions: MacOsOptions(
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),

At least for me adding any kind of groupId to MacOsOptions prevented it from working correctly. Removing it solved my issue.

    mOptions: MacOsOptions(
      groupId: 'com.domain.app', // NO NO! Did not matter what I used, any string made it not work.
      synchronizable: true,
      accessibility: KeychainAccessibility.first_unlock,
    ),

At least now it seems to work in my macOS builds as well and that is nice. Thanks all for your input and help. Got the answer in some example usage and discussion on X/Twitter in case anybody want to dig into that, here it is https://x.com/RydMike/status/1766876648001986614?s=20

rydmike avatar Mar 10 '24 17:03 rydmike

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

juliansteenbakker avatar Aug 13 '24 20:08 juliansteenbakker