问题描述
我有桌子:
@Entity()
@JsonSerializable()
class DateTimeStruct {
@JsonKey(ignore: true)
int id = 0;
DateTime time = DateTime.Now();
String? title;
DateTimeStruct();
factory DateTimeStruct.fromJson(Map<String,dynamic> json) => _$DateTimeStructFromJson(json);
Map<String,dynamic> toJson() => _$DateTimeStructToJson(this);
}
和
@JsonSerializable(explicitToJson: true)
@Entity()
class Event {
final Time = ToMany<DateTimeStruct>();
}
Store.Box<Event>().query().link(Event_.Time,DateTimeStruct_.time.greaterOrEqual(DateTime.Now().millisecondsSinceEpoch));
我明白了
The argument type 'QueryRelationMany<Event,DateTimeStruct>' can't be assigned to the parameter type 'QueryRelationProperty<Event,DateTimeStruct>'
错误信息。
第二个问题:DateTimeStruct_.time
解析为 QueryIntegerProperty<DateTimeStruct> time
,但它是 DateTime。是否在数据库中转换为整数?
解决方法
- 给定实体不适用于
pub run build_runner build
,因为Event
缺少id
- 您正在使用
ToMany
关系 - 如果您想为每个DateTimeStruct
存储多个Event
,您只想这样做 - 只是确保您知道。如果每个事件只有一次没有问题(从概念上讲,这对我来说是正确的),请使用ToOne
。 - 对于
ToMany
,您需要使用.linkMany()
而不是.link()
进行链接 - 这就是编译错误的来源。 - 是 Dart
DateTime
对象存储为毫秒时间戳(除非您使用@Property(type: PropertyType.dateNano) annotation
另行指定)。您需要这样查询它们 - 您似乎正确地这样做了。