程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了替换 .xml 文件后无法通过共享首选项获取数据大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决替换 .xml 文件后无法通过共享首选项获取数据?

开发过程中遇到替换 .xml 文件后无法通过共享首选项获取数据的问题如何解决?下面主要结合日常开发的经验,给出你关于替换 .xml 文件后无法通过共享首选项获取数据的解决方法建议,希望对你解决替换 .xml 文件后无法通过共享首选项获取数据有所启发或帮助;

我正在通过 Firebase/Google Drive API 实现备份/恢复数据。备份/恢复看起来正确。但我意识到共享首选项并没有像我预期的那样工作。我的意思是,正确的文件在那里:/data/data/MY_PACKAGE_name/shared_prefs/FlutterSharedPreferences.xml。我使用 file(path).readAsString() 检查文件是否有效。结果是正确的数据。但是当我通过共享首选项插件(即 getString() 方法)检查​​数据时,我无法获得正确的数据。我刚刚找到了旧数据。

所以我想我应该删除数据的缓存。然后我使用了下面的 reload() 方法。该方法看起来很适合我的目的。但是没有用。

/// Fetches the latest values from the host platform.
  ///
  /// Use this method to observe modifications that were made in native code
  /// (without using the plugin) while the app is running.
  Future<voID> reload() async {
    final Map<String,Object> preferences =
        await SharedPreferences._getSharedPreferencesmap();
    _preferenceCache.clear();
    _preferenceCache.addAll(preferences);
  }

然后我尝试了 clear() 方法 (clear old data -> download/replace data)。它也不起作用。

  /// Completes with true once the user preferences for the app has been cleared.
  Future<bool> clear() {
    _preferenceCache.clear();
    return _store.clear();
  }

我确定上传数据和下载数据都没有问题。但就是无法通过 Shared Preferences Plugin 获取正确的数据。

以下代码是 VIEw 通过 Shared Preferences Plugin 获取数据的示例。

...
// After downloaded data and replaced data on local disc
Elevatedbutton(
    onpressed: () async {
    final file = file(prefsPath);
    if (await file.exists()) {
        String contents = await file.readAsString();
        print(
            'This is ...shared_prefs/FlutterSharedPreferences.xml BEFORE reloading -> ...\n$contents');
        // correct data;
        print(screenviewmodel.getStringData()); // this is Share Preferences method.
        // old data;
    } else {
        print('prefsfile does NOT exist...');
    }

    screenviewmodel.reloadPrefs();
// Could not get what I expected even after shared_preferences.clear() old data then download/replace data.
    if (await file.exists()) {
        String contents = await file.readAsString();
        print(
            'This is ...shared_prefs/FlutterSharedPreferences.xml AFTER reloading -> ...\n$contents');
        // correct data;
        print(screenviewmodel.getStringData()); // this is Share Preferences method.
        // old data;
    } else {
        print('prefsfile does NOT exist...');
    }
    },child: Text('check data'))

...

FlutterSharedPreferences.xml 放置的路径正确,数据也正确。

但我根本无法使用数据。我在 VIEw 上发现的只是磁盘上不存在的旧数据。

我想知道如何删除缓存或如何通过共享首选项插件获取正确的数据。

#EDIT:

重建后,VIEw 显示正确的数据。 但我现在需要重新启动功能。我不喜欢那样...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

大佬总结

以上是大佬教程为你收集整理的替换 .xml 文件后无法通过共享首选项获取数据全部内容,希望文章能够帮你解决替换 .xml 文件后无法通过共享首选项获取数据所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:.xml替换