flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

Data of FlutterSecureStorage instance is null on iOS but not on Android

Open nizarmasri opened this issue 3 years ago • 14 comments

Hello, this code works on my Android app but not iOS. I tested it on a physical device and simulator.

class _MyHomePageState extends State<MyHomePage> {
  final storage = const FlutterSecureStorage();

  Future _containsKey() async {
    return storage.containsKey(key: "conciergeRowGuid");
  }

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: _containsKey(),
        builder: (context, snapshot) {
          return (snapshot.data == false)
              ? Scaffold(
                  backgroundColor: Colors.white,
                  body: Container(child: Text("not loaded"))
                )
              : FutureBuilder(
                  future: storage.read(key: "conciergeRowGuid"),
                  builder: (context, snapshot) {
                    if (snapshot.data == null) {
                      return SizedBox(
                        height: MediaQuery.of(context).size.height - 135,
                        width: MediaQuery.of(context).size.width,
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: const [
                            CircularProgressIndicator(
                              color: Color.fromARGB(255, 176, 64, 251),
                            ),
                          ],
                        ),
                      );
                    }
                    return Container(child: Text("loaded"));
                  });
        });
  }

In the iOS app, snapshot.data is always null. Digging into the iOS code of the Flutter Secure Storage, the functions of containsKey, write, read, readAll, deleteAll, all have groupId as nil. Also, OSStatus status has the error code 25300 which means errSecItemNotFound . And the return NSString *value is also nil. There is a similar issue #107 but the fix suggested is not working for me.

Specs: Flutter 2.10.4 flutter_secure_storage: ^5.0.2 MacOS Big Sur 11.6 Tested on:

  • iPhone 13 Simulator iOS 15.0
  • Physical iPhone 11 iOS 14.6
  • Physical iPhone SE (2nd gen) iOS 15.4.1

nizarmasri avatar May 04 '22 18:05 nizarmasri

I am just having the same problem, android and web can normally get the value, on Ios, although the contains(key) is true, the read() gets null

oupat avatar Jul 15 '22 11:07 oupat

when it will be fixed? still facing it. @oupat @nizarmasri did you find any solution?

Same here!

fdelucchijr avatar Sep 08 '22 03:09 fdelucchijr

Same!

localboss-git avatar Sep 16 '22 12:09 localboss-git

Same here!

PD: In my case the error only appears when I add flavorz in mi app. Without the native flavors everything goes smoothly.

fdelucchijr avatar Sep 16 '22 18:09 fdelucchijr

Sorry for the late response. 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?

juliansteenbakker avatar Sep 30 '22 21:09 juliansteenbakker

Currently I followed the first step in this:

# ios/Runner/Runner.entitlements

+ <key>keychain-access-groups</key>
+   <array>
+      <string>$(AppIdentifierPrefix)*</string>
+   </array>

answer and everything working. @juliansteenbakker I will check the version as soon I have a break. You think that version would replace the fix I added above?

fdelucchijr avatar Oct 01 '22 03:10 fdelucchijr

You think that version would replace the fix I added above?

No, I tried and the fix is also required :c

fdelucchijr avatar Oct 01 '22 03:10 fdelucchijr

v6.1.0-beta.1 fixed the issue for me.

cirediew avatar Oct 06 '22 14:10 cirediew

I had a similar issue but only when the app went to the background and the phone was locked, turned out the keychain gets locked after a while

const FlutterSecureStorage(
        iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock),
      ),

Resolved the issue for me

mrRedSun avatar Jun 29 '23 08:06 mrRedSun

For me the main issue went away, when I set the MinSDKversion for IOS to 13 (see https://docs.amplify.aws/flutter/start/project-setup/platform-setup/#enable-keychain)

tbecker avatar Dec 16 '23 16:12 tbecker

For us, both read is always null, contains is always false and write doesn't throw any error (So assuming that it's storing). This issue is replicable across all scenarios on iOS 17.2 Emulator.

We have even tried these steps

  • iOptions: IOSOptions(accessibility: KeychainAccessibility.first_unlock),
  • Set MinSDKversion for IOS to 13
  • $(AppIdentifierPrefix)*

None of them has fixed the issue.

pvsvamsi avatar Dec 29 '23 12:12 pvsvamsi

For me the main issue went away, when I set the MinSDKversion for IOS to 13 (see https://docs.amplify.aws/flutter/start/project-setup/platform-setup/#enable-keychain)

This solve issue that can't persist data on macOS for me.

codelovercc avatar Jan 02 '24 02:01 codelovercc

I am using flutter_secure_storage: ^9.0.0. I am getting the same error. FlutterSecureStorage instance is null on ios but not in Andriod.

anupraz avatar Jan 25 '24 03:01 anupraz

isn't there related code in develop re: protected data availability

neiljaywarner avatar Feb 20 '24 22:02 neiljaywarner