我一直在尝试使用 dart 中的共享首选项保存列表列表。例如,我有一个列表 List data = [["dave","21","M"],["steven","22","F"]]
我正在尝试按原样保存和加载,但应用程序不断抛出 Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<String>'
我尝试使用 List new_data = data.expand((i) => i).toList();
将二维列表转换为列表然后保存它。但是,即使它是一个仅包含字符串的列表,异常仍然存在。
最佳答案
我们可以使用 jsonEncode
和 jsonDecode()
来获取列表:
import 'package:shared_preferences/shared_preferences.dart';
List data = [["dave","21","M"],["steven","22","F"]];
handleData() async {
var prefs = await SharedPreferences.getInstance();
prefs.setString('key', jsonEncode(data)); // Encode the list here
print(jsonDecode(prefs.getString('key'))); // Decode then print it out
}
关于android - 如何使用 dart/flutter 中的共享首选项保存和获取列表列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56198413/