【问题标题】:How to access flutter Shared preferences on the android end (using java)如何在android端访问flutter共享首选项(使用java)
【发布时间】:2019-03-18 15:50:48
【问题描述】:

我在 dart 中有以下函数来设置特定的布尔偏好值。

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

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

现在在原生 android 端,我应该使用这个共享偏好值。这是我到目前为止所做的。

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

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

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

而且它只呈现 Android 创建的值(使用 java)。

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

【问题讨论】:

    标签: android flutter sharedpreferences flutter-dependencies


    【解决方案1】:

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

    你可以做的是:

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

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

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

    【讨论】:

    • 好人,好答案
    猜你喜欢
    • 2019-08-09
    • 2021-04-19
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2016-02-14
    相关资源
    最近更新 更多