我在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条件永远不会被评估为真。我还尝试使用下面的代码打印所有现有的首选项值。
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”键,你必须添加“扑腾”。字首:
String value = prefs.getString("flutter."+key, null);
有一个名为shared_preferences的精彩包,由Flutter团队开发,可以在iOS和Android上处理这个问题而无需触及本机代码库!
我不知道你是否在代码中使用它,但如果没有,我强烈建议你查看它。