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

Viewed 3620

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).

5 Answers

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.

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", "");

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/

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.

I think the third party plugin is just fine. The best thing about flutter is its amazing community that keeps contributing thoroughly to the ecosystem. That said, I would not recommend keeping using the non-official library. So you could test migrating your shared preferences behind the scenes.

  • In your initState query for flutter shared preferences.
  • If they exist, skip.
  • If they don't exist, query for native shared preferences, copy if exist

And done. This will help with the migration IMHO.

My choice is to treat it just as a file.
In flutter read the android shared preference file

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

take the values in the way you handle other xml file.

If you want to make some changes, don't use sharedPrefs.put, Instead write into the file (/data/data/patavinus.patavinus/shared_prefs/storage.xml ) using file's write method in flutter

Related