我不能在我的框中保存Map类型,songInfo
属性
@Entity()
class PlaylistItem {
int id = 0;
int recentIndex;
Map<String, Object> songInfo;
PlaylistItem({
this.recentIndex = 0,
this.songInfo = const <String, Object>{} ,
});
}
当我运行:flutter pub run build_runner build
时,它忽略了songInfo
警告:skipping property 'songInfo' in entity 'PlaylistItem', as it has an unsupported type: 'Map<String, Object>'
为什么Map是不支持的类型?我如何存储地图?我需要帮助(与一个例子将是欢迎)
您需要创建一个自定义转换器,如下所示https://docs.objectbox.io/advanced/custom-types
最好的办法可能是将其转换为json并将其存储为String。未经测试,但您需要以下内容。虽然映射的Object部分使它变得有点棘手,但您还必须编写一种方法将对象序列化为json/string
String? get dbSaveSongInfo=>
songInfo== null ? null : json.encode(songInfo);
set dbSaveSongInfo(String? value) {
if (value == null) {
songInfo= null;
} else {
songInfo= Map.from(
json.decode(value).map((k, v) => MapEntry(k as String, v(handle your 2nd conversion here));
}
}