android - 如何在android端访问flutter Shared preferences(使用java)

标签 android flutter sharedpreferences flutter-dependencies

我在 dart 中有以下函数来设置特定的 bool 首选项值。

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

此函数设置 isPersistentNotificationEnabled 首选项的值。

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

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

并且 if 条件永远不会被评估为真。我还尝试使用下面的代码打印所有现有的偏好值。

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

而且它只显示 Android 创建的值(使用 java)。

非常感谢您在访问首选项值方面提供的任何帮助。 谢谢。

最佳答案

抱歉,这个答案来晚了,刚刚从 flutter github 页面上得到了答案。

你可以做的是:

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

那么如果你想阅读,例如“myValue”键,你必须添加“flutter”。前缀:

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

关于android - 如何在android端访问flutter Shared preferences(使用java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55225207/

相关文章:

java - Intent 导致 Activity 崩溃

flutter - 将数据获取到appBar-Flutter

flutter - 如何在Flutter中将渐变颜色设置为图标?

Android:DialogPreference 中的可点击超链接

安卓 : How to set edittext text just in first touch after activity launch

SharedPreference 的 Android handle 注入(inject)

dart - Flutter:共享偏好

android - 密码保护 Android 中的首选项

java - FragmentPagerAdapter 中的 RecyclerView 泄漏

flutter - Flutter 支持负边距吗?