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