Dim*_*imZ 2 flutter objectbox flutter-objectbox
我有一个带有 Map 属性的类,但它似乎不存储该属性。所有其他属性都存储良好:
@JsonSerializable()
@Entity()
class PossessionStats {
@JsonKey(defaultValue: 0)
int id;
String cardUuid;
int total;
Map<String, int>? totalByVersion;
CardPossessionStats({
this.id = 0,
required this.cardUuid,
this.total = 0,
this.totalByVersion,
});
}
Run Code Online (Sandbox Code Playgroud)
当我保存一个时:
stats = PossessionStats(cardUuid: card.uuid);
if (stats.totalByVersion == null) {
stats.totalByVersion = Map();
}
stats.totalByVersion!
.update(version, (value) => quantity, ifAbsent: () => quantity);
stats.total = stats.totalByVersion!.values
.fold(0, (previousValue, element) => previousValue + element);
box.put(stats);
Run Code Online (Sandbox Code Playgroud)
当我得到结果时(刷新后),我可以看到total是正确的,但totalByVersion仍然是空的。
谢谢!
对于不受支持的类型,您需要添加一个“转换器”,如文档(自定义类型)中所述。您可以根据您的代码调整文档,例如使用json作为存储格式:
@JsonSerializable()
@Entity()
class PossessionStats {
@JsonKey(defaultValue: 0)
int id;
String cardUuid;
int total;
Map<String, int>? totalByVersion;
String? get dbTotalByVersion =>
totalByVersion == null ? null : json.encode(totalByVersion);
set dbTotalByVersion(String? value) {
if (value == null) {
totalByVersion = null;
} else {
totalByVersion = Map.from(
json.decode(value).map((k, v) => MapEntry(k as String, v as int)));
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1313 次 |
| 最近记录: |