我正在尝试将地图保存为共享的首选项并重新加载,我在保存时对地图进行编码,在加载时进行解码,但是我无法将解码json的值设置回我保存的原始地图,我的json解码返回的数据是什么?
Map<String, int> purchasedItems = {Sweet Pepper: 0, Lemon: 0, Eggplants: 0, Colorful Pepper: 0, Kiwi: 0, Fava Bean: 0, GrapeLeaf dolma: 0, Potatoes: 0, Spicy Pepper: 0, Grapefruit: 0, Tomatoes: 0, Cherries: 0, Red raddish: 0, Carrots: 0, Dolma: 0};
Map<String, int> savedPurchasedItems = {};
void savePurchasesPrefs(Map<String, int> purchasedItems) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var something = jsonEncode(purchasedItems);
var savePurchases = prefs.setString("savedPurchasedItems", something);
print("saved purchases prefs $savePurchases");
}
void loadPurchasesPrefs() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
var something = await prefs.get("savedPurchasedItems");
print("loaded items: $something");
var yee = jsonDecode(something);
purchasedItems = yee;
print("yee : $yee");
}
答案 0 :(得分:0)
通过将purchaseItems的类型更改为“到处映射”来修复它,并且效果很好。