问题:
无法将自定义模型数据作为共享首选项保存到列表中。
购物车模型:
Cart Class{
String id;
Product product;
String product_name;
double quantity;
String userId;
Cart();
Map toMap() {
var map = new Map<String, dynamic>();
map['product_name'] = product.name;
map["id"] = id;
map["quantity"] = quantity;
map["user_id"] = userId;
return map;
}
}
用法:
:
:
List<Cart> catchedCart = new List(2000); //initialization
:
:
updateSharedListCart(cartToBePassed, position) // cartToBePassed is an object of type Cart &
// position where to be added
:
:
:
updateSharedListCart(Cart cartToAppendDelete, int pos) async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList("catchedCart[pos]", cartToAppendDelete);
setState(() {
catchedCart[pos]= prefs.getString('catchedCart[pos]');
});
} catch (e) {
print(e);
}
}
因此Flutter返回了错误:“ The argument type 'Cart' can't be assigned to the parameter type 'List<String>'
”
有人可以提供语法帮助,以使用我的自定义模型来设置共享首选项吗?一个样本会很有帮助。