androidiosfluttersharedpreferencesnsuserdefaults

How to access iOS UserDefaults stored data in Flutter SharedPrefrences


I have an iOS app already on Store, Now planning to replace it with Flutter with new version release.

I want to get access native app's UserDefaults data in Flutter code.

Using the way suggested in Flutter docs I m getting null value. What I have tried is:

In my pubspec.yaml file :

dependencies:
  shared_preferences: ^0.5.12+4

Im my home.dart class file I'm importing header :

import 'package:shared_preferences/shared_preferences.dart';

And this is how I m trying to access data stored in iOS app from native app, I m using same bundle ID (Package ID) in flutter project to overwrite the app and it is successfully overwriting.

@override
void initState() {
    super.initState();
    getFromLocalMemory('user').then((value) =>
      userInfo = value
  );
}



Future<String> getFromLocalMemory(String key) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String user = prefs.getString(key);
    return user;
}

It always returns null. While 'user' key has data when getting from iOS native app.


Solution

  • In that case, I suggest you use platform channel method

    platform channels

    iOS doesn't seem to allow flutter framework to access NSUserDefaults, when it didn't create it from the beginning,..

    I hope it will work for you.