Hou*_*sem 1 sharedpreferences flutter
我想获得除两个键之外的所有 SharedPreferences 键
我不能使用 getString(Key) 方法,因为有 N 个键。
Future<List<Widget>> getAllPrefs() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
// I won't print those two keys neither remove them
// prefs.remove("lib_cached_image_data");
// prefs.remove("lib_cached_image_data_last_clean");
prefs.setString("Market", "position");
prefs.setString("Home", "position");
return prefs.getKeys().map<Widget>((key) {
//this is incorrect
if (key != "lib_cached_image_data" && key != "lib_cached_image_data") {
ListTile(
title: Text(key),
subtitle: Text(prefs.get(key).toString()),
);
}
}).toList(growable: false);
}
Run Code Online (Sandbox Code Playgroud)
我期望输出:除 ("lib_cached_image_data",value) , ("lib_cached_image_data_last_clean",value) 之外的所有 ("Keys", "Values")
你可以使用where. 不要忘记返回ListTile。
final SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.getKeys().where((String key) => key != "lib_cached_image_data" && key != "lib_cached_image_data").map<Widget>((key) {
return ListTile(
title: Text(key),
subtitle: Text(prefs.get(key).toString()),
);
}).toList(growable: false);
Run Code Online (Sandbox Code Playgroud)