如何在列表中保存共享首选项?

时间:2020-04-03 12:54:10

标签: flutter dart

我有两个类,在第一类中设置了sharedPrefrence:

[{"name":"alex","code":"12345"}]

我的共享首选项设置方法:

  Future _shared() async {
  final _customer = {
  "name": _controller1.text,
  "code": _controller2.text,
};
  List<Map<String, dynamic>> customers = [];
  customers.add(_customer);


 final customerEncode = jsonEncode(customers);

 SharedPreferences pref = await SharedPreferences.getInstance();
 pref.setString("list_customer", customerEncode);

 print(customerEncode);
}

并在第二班显示此列表,我想回到第一班并输入名称和代码,将它们添加到上一个列表(保留以后的数据),如下所示:

[{"name":"alex","code":"12345"},{"name":"john","code":"98765"}]

我该怎么做?

1 个答案:

答案 0 :(得分:0)

创建 var s1 = await agent.SynchronizeAsync(progress); 模型:

  System.NullReferenceException: Object reference not set to an instance of an object
  at SQLitePCL.raw.sqlite3_open_v2 (SQLitePCL.utf8z filename, SQLitePCL.sqlite3& db, 
  System.Int32 flags, SQLitePCL.utf8z vfs) [0x00000] in <15ecb38d58394d7b88b3f841a7dda078>:0
  at SQLitePCL.raw.sqlite3_open_v2 (System.String filename, SQLitePCL.sqlite3& db, 
  System.Int32 flags, System.String vfs) [0x0000e] in <15ecb38d58394d7b88b3f841a7dda078>:0
  At Microsoft. Data. Sqlite. SqliteConnection. Open () [0x00122] in 
 <9ffe4c48f3134a7b905b5da527410f26>:0
 at System.Data.Common.DbConnection.OpenAsync (System.Threading.CancellationToken 
 cancellationToken) [0x00011] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.0/src/Xamarin.iOS/external/corefx/src/.    System.Data.Common/src/System/Data/Common/DbConnection.cs:122

然后,创建一个添加到您的Customer列表中的方法:

class Customer{
  Customer({this.name, this.code});

  String name;
  String code;

  Customer.fromMap(json)
    : name = json['name'].toString(),
      code= json['code'].toString();
}