flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

onCupertinoProtectedDataAvailabilityChanged listener is not returning any value(true or false) even when isCupertinoProtectedDataAvailable is true

Open aswinranjith-iouring opened this issue 1 year ago • 2 comments
trafficstars

Hi, I am facing this where I have protected data stored in my keychain using flutter_secure_storage and I need to check if protected data is available or not then whenever the data becomes available I need to get the values from the keychain. here is my code. but the issue is even when isProtectedDataAvailable is true the listener is not returning anything. Is this an implementation issue or am I missing something? Please tell me how to implement this properly. The package version I am using is 9.2.2

final isProtectedDataAvailable =
    await _storage.isCupertinoProtectedDataAvailable();

if (isProtectedDataAvailable ?? false) {
  _storage.onCupertinoProtectedDataAvailabilityChanged?.listen(
    (availability) {
      if (availability) {
        LogManager().log('availability true');
      } else {
        // Handle normal flow
        LogManager().log('availability false');
      }
    },
    onError: (e) {
      LogManager().log('error occurred $e');
    },
    cancelOnError: true,
    onDone: () {
      LogManager().log('Done');
    },
  );
}

aswinranjith-iouring avatar Jun 06 '24 06:06 aswinranjith-iouring

Here is flutter doctor -v information [✓] Flutter (Channel stable, 3.19.6, on macOS 14.0 23A344 darwin-arm64, locale en-IN) • Flutter version 3.19.6 on channel stable at /Users/aswinranjith/Documents/flutter 3.19.6/flutter • Upstream repository https://github.com/flutter/flutter.git • Framework revision 54e66469a9 (7 weeks ago), 2024-04-17 13:08:03 -0700 • Engine revision c4cd48e186 • Dart version 3.3.4 • DevTools version 2.31.1

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/aswinranjith/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 15.4) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15F31d • CocoaPods version 1.14.3

[✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2023.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160)

[✓] VS Code (version 1.89.1) • VS Code at /Users/aswinranjith/Downloads/Visual Studio Code.app/Contents • Flutter extension version 3.82.0

[✓] Connected device (4 available) • Aswin’s iPhone (mobile) • hiding device id • ios • iOS 17.5.1 21F90 • iPhone 15 (mobile) • hiding device id • ios • com.apple.CoreSimulator.SimRuntime.iOS-17-4 (simulator) • macOS (desktop) • macos • darwin-arm64 • macOS 14.0 23A344 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 125.0.6422.142 ! Error: Browsing on the local area network for Aswin’s iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27)

[✓] Network resources • All expected network resources are available.

• No issues found!

aswinranjith-iouring avatar Jun 06 '24 06:06 aswinranjith-iouring

I am facing the same issue. There doesn't seem to be any values being returned by the listener.

mcg95 avatar Jun 10 '24 07:06 mcg95

import 'dart:io';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

extension StorageAvailability on FlutterSecureStorage {
  /// waiting availability, especially for iOS
  ///
  /// for example, iOS secure storage may not available due to prewarming
  /// https://developer.apple.com/documentation/uikit/app_and_environment/responding_to_the_launch_of_your_app/about_the_app_launch_sequence#3894431
  ///
  /// https://developer.apple.com/documentation/uikit/uiapplication/1622925-isprotecteddataavailable
  Future<void> waitingAvailability() async {
    if(Platform.isIOS) {
      if(await isCupertinoProtectedDataAvailable() == false) {
        // waiting for protectedDataDidBecomeAvailableNotification
        // https://developer.apple.com/documentation/uikit/uiapplication/1623039-protecteddatadidbecomeavailablen
        await onCupertinoProtectedDataAvailabilityChanged!.firstWhere((element) => element);
      }
    }
  }
}

justprodev avatar Jul 11 '24 01:07 justprodev

@justprodev Thanks for the reply. But my question is still not answered. onCupertinoProtectedDataAvailabilityChanged method is not returning anything. Should I only read the values from the secure storage if this onCupertinoProtectedDataAvailabilityChanged method returns true? That's where my confusion is. The documentation for this change is not proper. So please mention how to use this properly.

aswinranjith-iouring avatar Jul 11 '24 05:07 aswinranjith-iouring

You should listen for notifications only if await isCupertinoProtectedDataAvailable() == false

else you will not receive any notification.

Or, in other words, if await isCupertinoProtectedDataAvailable() == true then you already has access to secure storage

justprodev avatar Jul 11 '24 08:07 justprodev

Okay. Thank you so much @justprodev

aswinranjith-iouring avatar Jul 12 '24 05:07 aswinranjith-iouring