我在 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/