我有以下问题:现在,PlayStore中有一个应用程序是用本机代码(包括iOS和Android)编写的,我正计划将其迁移。我的目标是用户不会注意到引擎盖下有任何变化,但可以像以前一样继续使用该应用程序。为此,我还需要迁移共享首选项。但是,这非常困难。在本机Android应用程序中,我像这样存储了相关的共享首选项:
SharedPreferences sharedPrefs = context.getSharedPreferences(
"storage",
Context.MODE_PRIVATE
);
sharedPrefs.putString('guuid', 'guuid_value');
editor.apply();
将导致在此路径上创建文件:
/data/data/patavinus.patavinus/shared_prefs/storage.xml
具有此内容:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="guuid" value="guuid_value" />
</map>
如果我在Flutter中使用shared_prefences来通过执行以下操作获取此值:
final sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.getString('guuid');
它返回null,因为它寻找
/data/data/patavinus.patavinus/shared_prefs/FlutterSharedPreferences.xml
,这是在Flutter中使用shared_preferences来存储共享首选项时要写入的文件。因为共享首选项是在本机应用程序上下文中编写的,所以文件显然不存在。
是否有任何方法可以使Flutter无需使用平台通道来寻找/data/data/patavinus.patavinus/shared_prefs/storage.xml
?
我知道这是如何工作的,就像这里提到的那样:How to access flutter Shared preferences on the android end (using java)。这种方法很简单,因为在Android中,您可以选择在Flutter的前缀之前添加前缀。但是,在Flutter中您不能这样做。
[我也知道此插件:https://pub.dev/packages/native_shared_preferences,但是,我不相信推荐使用第三方插件。另外,我已经在多个资源文件中分散了相关的共享首选项。在此插件中,您只能设置一个(通过指定字符串资源flutter_shared_pref_name
)。
根据建议here,您可以获得原始文件的内容并将其复制到新文件。当用户首次升级到Flutter应用时,您可以将内容复制到Flutter的存储文件中。