Read shared preferences in Flutter app that were previously stored in a native app

2,931

Solution 1

Since there was no satisfying answer to my question that works for both of the major operating systems and taking into account the distribution over multiple resource files, I have written a platform channel solution myself.

On Android (Kotlin) I let the caller provide a "file" argument because it's possible to have the data spread over multiple resource files (like I described in my question).

package my.test

import android.content.*
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity: FlutterActivity() {
    private val CHANNEL = "testChannel"

    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler {
            call, result ->
            when (call.method) {
                "getStringValue" -> {
                    val key: String? = call.argument<String>("key");
                    val file: String? = call.argument<String>("file");

                    when {
                        key == null -> {
                            result.error("KEY_MISSING", "Argument 'key' is not provided.", null)
                        }
                        file == null -> {
                            result.error("FILE_MISSING", "Argument 'file' is not provided.", null)
                        }
                        else -> {
                            val value: String? = getStringValue(file, key)
                            result.success(value)
                        }
                    }
                }
                else -> {
                    result.notImplemented()
                }
            }
        }
    }

    private fun getStringValue(file: String, key: String): String? {
        return context.getSharedPreferences(
                file,
                Context.MODE_PRIVATE
        ).getString(key, null);
    }
}

On iOS (Swift) this is not necessary as I'm working with UserDefaults

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    let controller : FlutterViewController = window?.rootViewController as! FlutterViewController
    let platformChannel = FlutterMethodChannel(
      name: "testChannel",
      binaryMessenger: controller.binaryMessenger
    )

    platformChannel.setMethodCallHandler({
      [weak self] (call: FlutterMethodCall, result: FlutterResult) -> Void in
      guard call.method == "getStringValue" else {
        result(FlutterMethodNotImplemented)
        return
      }

      if let args: Dictionary<String, Any> = call.arguments as? Dictionary<String, Any>,
        let number: String = args["key"] as? String, {
        self?.getStringValue(key: key, result: result)
        return
      } else {
        result(
          FlutterError.init(
            code: "KEY_MISSING",
            message: "Argument 'key' is not provided.", 
            details: nil
          )
        )
      }
    })

    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
    
private func getStringValue(key: String, result: FlutterResult) -> String? {
  let fetchedValue: String? = UserDefaults.object(forKey: key) as? String
  result(fetchedValue)
}

I'd wish for the SharedPreferences package to add the possibility of omitting the flutter prefix, enabling the developer to migrate content seamlessly from native apps.

I also wrote a blog post that explains the problem and the solution in a little bit more details: https://www.flutterclutter.dev/flutter/tutorials/read-shared-preferences-from-native-apps/2021/9753/

Solution 2

As suggested here you can get the native file's content and copy it to a new file. You can copy the content to flutter's storage file when the user upgrades to your flutter app for the first time.

Solution 3

Best option is to store shared preferences in native part in same file as SharedPreferences plugin does it. So it means to do like that:

  1. In Java code replace your SharedPreferences code, with same key, as it is in plugin: SharedPreferences sharedPref = context.getSharedPreferences("FlutterSharedPreferences", Context.MODE_PRIVATE);
  2. In native part save all preferences with prefix "flutter.". So in native part you will get needed preferences like this: String test = sharedPref.getString("flutter.test", "");
Share:
2,931
Schnodderbalken
Author by

Schnodderbalken

I am the liquid that comes out of your nose. My shape is a timber. In Germany we call that thing SCHNODDERBALKEN!

Updated on December 21, 2022

Comments

  • Schnodderbalken
    Schnodderbalken over 1 year

    I have the following problem: right now there is an app in the PlayStore that is written in native code (both iOS and Android) which I'm planning on migrating to flutter. My aim is that the users don't notice there were changes under the hood but can continue using the app like before. For that I need to migrate the shared preferences as well. This is, however, quite difficult. In the native Android application I stored the relevant shared preference like this:

    SharedPreferences sharedPrefs = context.getSharedPreferences(
        "storage",
        Context.MODE_PRIVATE
    );
    
    sharedPrefs.putString('guuid', 'guuid_value');
    editor.apply();
    

    which results in a file being created at this path:

    /data/data/patavinus.patavinus/shared_prefs/storage.xml
    

    with this content:

    <?xml version='1.0' encoding='utf-8' standalone='yes' ?>
    <map>
        <int name="guuid" value="guuid_value" />
    </map>
    

    If I use shared_prefences in Flutter to obtain this value by doing this:

    final sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.getString('guuid');
    

    it returns null because it looks for

    /data/data/patavinus.patavinus/shared_prefs/FlutterSharedPreferences.xml which is the file that is written to when using shared_preferences in Flutter to store shared preferences. Because the shared prefs were written in native app context, the file is obviously not there.

    Is there any way to tell Flutter to look for /data/data/patavinus.patavinus/shared_prefs/storage.xml without having to use platform channel?

    I know how this works the other way around like it's mentioned here: How to access flutter Shared preferences on the android end (using java). This way is easy because in Android you can choose to prepend Flutter's prefix. However, in Flutter you can't.

    I am also aware of this plugin: https://pub.dev/packages/native_shared_preferences however, I can't believe that a third party plugin is the recommended way. Also, I have spread the relevant shared preferences across multiple resource files. In this plugin, you can only set one (by specifying the string resource flutter_shared_pref_name).

  • Schnodderbalken
    Schnodderbalken almost 4 years
    This is indeed a nice suggestion for the Android platform. The major drawback ist however that it does not work for iOS this way :(.
  • Sami Haddad
    Sami Haddad almost 4 years
    On iOS it seems they only prefix the keys with flutter.. So creating a platform channel would be easier than you think, you can create a similar function to this without the flutter prefix.
  • Raveesh Agarwal
    Raveesh Agarwal almost 4 years
    to be honest this is not the ideal way of doing things. I am interested in making a plugin for this.
  • prabhu r
    prabhu r almost 3 years
    @Schnodderbalken I don't know about iOS since I've not worked on it?
  • Schnodderbalken
    Schnodderbalken almost 3 years
    I understand. But that's what my question is about. I am seeking for a solution that works for both OS.
  • prabhu r
    prabhu r almost 3 years
    If you know how to read write in a file in android & iOS that's enough. put a if condition to check if the app runs on android, and write it in a file using native method(java) & method channel , in else block do it iOS native way & method channel
  • Schnodderbalken
    Schnodderbalken over 2 years
    This is not an option as I don't have access to the native apps and can not migrate the keys. I only have the existing native apps and the desire to migrate to Flutter.
  • Andris
    Andris over 2 years
    @Schnodderbalken You can migrate to flutter only by creating apps on flutter. And if you are making apps on flutter, you can still write native code.
  • Schnodderbalken
    Schnodderbalken over 2 years
    You suggest "store shared preferences in native part in same file as SharedPreferences plugin does it". But the preferences are ALREADY stored. There are native apps out there that have NOT stored it with flutter prefix. So how do I access them? That's what my question is about.
  • Andris
    Andris over 2 years
    @Schnodderbalken You can read them using specific preferences key and store them in new file, which one is using same key as flutter is using.
  • johnw182
    johnw182 about 2 years
    This is indeed annoying and I cannot believe you and I are the only two people on earth migrating from native Android to Flutter and want to keep preferences? This is a major flaw on the developers especially considering how widely SharedPreferences is used. Thanks for asking this Q and providing your answer.