【发布时间】:2021-12-30 22:19:16
【问题描述】:
我有自定义对象列表,这些对象也具有非原始数据类型的字段。我想将这些对象的list 存储在SharedPreferences 中。这是我的List,也是简单的Data:
@JsonSerializable()
class MedicamentsBase {
final List<Medicament> data;
const MedicamentsBase({
required this.data,
});
factory MedicamentsBase.fromJson(Map<String, dynamic> json) =>
_$MedicamentsBaseFromJson(json);
Map<String, dynamic> toJson() => _$MedicamentsBaseToJson(this);
}
@JsonSerializable()
class Medicament {
@JsonKey(name: '_id')
String id;
String name;
int dosageAmount;
String dosageType;
DateTime startDate;
DateTime endDate;
List<DateTime> timesOfDay;
String intakeContext;
bool notify;
int notificationId;
Medicament({
required this.name,
required this.dosageAmount,
required this.dosageType,
required this.startDate,
required this.endDate,
required this.timesOfDay,
required this.intakeContext,
required this.notify,
required this.notificationId,
required this.id,
});
factory Medicament.fromJson(Map<String, dynamic> json) =>
_$MedicamentFromJson(json);
Map<String, dynamic> toJson() => _$MedicamentToJson(this);
}
我已经对它们进行了序列化。但如果我尝试像这样存储MedicamentsBase:
static Future<void> setMedicaments(List<Medicament> medicaments) async {
final SharedPreferences _sharedPreferences =
await SharedPreferences.getInstance();
await _sharedPreferences.setStringList(
'key',
medicaments.map((medicament) => json.encode(medicament)).toList(),
);
}
如果尝试像这样取回列表,事情将无法按预期工作:
static Future<List<Medicament>> getMedicaments() async {
final SharedPreferences _sharedPreferences =
await SharedPreferences.getInstance();
return MedicamentsBase.fromJson(
_sharedPreferences.getStringList('key') as Map<String, dynamic>,
).data;
}
我认为这是因为Medicament 中也有Strings 和DateTimes。在Shared Preferences中存储和获取此类数据的正确方法是什么?
【问题讨论】:
-
是否有“必须”使用 SharedPreferences?因为您可以使用 hive 轻松处理这种情况。
-
@Xoltawn 不是绝对必须的,但绝对是首选。我读过关于 Hive 的文章,但我认为实施它会更加努力,而且在这种情况下时间/金钱很少:D
-
在我看来这是值得的。如果你潜入它,你会发现它并不像第一眼看上去那么可怕。但我没有在 SharedPreferences 中完成这个场景,所以我对此没有意见。
-
我会看看它,因为它看起来很有希望。但是现在我会坚持SF。无论如何谢谢:)
-
如何将列表编码为单个字符串,而不是将列表项编码为字符串列表?
标签: json flutter dart sharedpreferences encode