如何在Android端访问Flutter共享首选项(使用Java) [英] How to access flutter Shared preferences on the android end (using java)

查看:120
本文介绍了如何在Android端访问Flutter共享首选项(使用Java)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在dart中具有以下功能来设置特定的布尔首选项值.

I have the following function in dart to set a particular boolean preference value.

_switchPersistentNotifications() {
  setState(() {
    isPersistentNotificationEnabled = !isPersistentNotificationEnabled;
  });
  widget.preferences.setBool(
      "isPersistentNotificationEnabled", isPersistentNotificationEnabled);
}

此功能设置isPersistentNotificationEnabled首选项的值.

This function sets the value of isPersistentNotificationEnabled preference.

现在在本机android端,我应该使用此共享的首选项值.到目前为止,这是我所做的.

Now on the native android end, I am supposed to use this shared preference value. Here's what I have done so far.

SharedPreferences preferences =
                    PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                // check if the state change notification is to be shown
                if (preferences.getBoolean("flutter.isStateChangeNotificationEnabled", false)) {
                    showConnectionStateChangeNotification();
                }

并且if条件永远不会被评估为true.我还尝试使用下面的代码打印所有现有的首选项值.

And the if condition never gets evaluated to true. I also tried printing all the existing preference values using the code below.

Map<String, ?> allEntries = preferences.getAll();
            for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
                Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
            }

并且它仅显示Android(使用java)创建的值.

And it only presents the values that the Android has created (using java).

在访问首选项值方面的任何帮助都将不胜感激. 谢谢.

Any help in accessing the preference values is greatly appreciated. Thank You.

推荐答案

很抱歉,这个答案来得太晚了,刚从github扑扑的页面得到了答案.

Sorry this answer is coming late, just got the answer from flutter github page.

您可以执行以下操作:

SharedPreferences prefs = getSharedPreferences("FlutterSharedPreferences", MODE_PRIVATE);

然后,如果您想阅读例如在"myValue"键中,您必须添加"flutter".前缀:

then if you want to read e.g. "myValue" key, you have to add "flutter." prefix:

String value = prefs.getString("flutter."+key, null);

这篇关于如何在Android端访问Flutter共享首选项(使用Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆