我一直在尝试使用 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
and 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
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)