我将对象设置为:{“ name”:“ alex”,“ code”:“ 123”} 进入sharedPrefrence Calss A:
var resBody = {};
resBody["name"] = name.text;
resBody["code"] = pass.text;
str = json.encode(resBody);
print(str);
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("list_customer", str);
,并在另一个类中获取此sharedPrefrence时 将共享的价值添加到列表B类:
customer = (prefs.getString('list_customer'));
Map<String, dynamic> user = jsonDecode(customer);
_customer.nameFamily = user['name'];
_customer.code = user['code'];
_list.add(_customer);
,我想知道如何像这样将共享的新值设置到上一个列表中: [{“ name”:“ alex”,“ code”:“ 123”},{“ name”:“ john”,“ code”:“ 128”}]
答案 0 :(得分:0)
在下面查看示例
$key+=1;
答案 1 :(得分:0)
要存储多个客户,您需要List
而不是Map
。
List
List<Map<String, dynamic>> customers = [];
final customer = {
"name": name.text,
"code": pass.text,
};
customers.add(customer);
customers
列表final customersString = json.encode(customers);
customers
列表SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("list_customer", customersString);
假设您存储了一位客户,现在您需要存储另一位客户。
customers
列表,然后对其进行解码String customersString = await prefs.getString('list_customer');
final customers = json.decode(customersString);
customers.add({
"name": 'Amir',
"code": 'SWF2022',
});
祝你好运