渡槽 ORM 托管对象的瞬态属性被保留



我有一个使用ORM的渡槽项目,数据模型如下:

class _Thing {
@primaryKey
int id;
String first;
String second;
}
class Thing extends ManagedObject<_Thing> implements _Thing {
@Serialize()
OtherThing get firstAndSecond() {
// return some value computed from first and second
}
@Serialize()
set firstAndSecond(OtherThing firstAndSecond) {
// set first and second based on some computation
}
}

根据瞬态属性的文档,使用@Serialize()进行注释应使此模型能够序列化/反序列化。它还说ManagedObjects 中的属性不会持久化,但是当我运行服务器时,我收到错误:

Data Model Error: Property 'firstAndSecond' on 'Thing' has an unsupported type.

如果我删除@Serialize(),它不会尝试保留它,但我无法序列化/反序列化此对象。

关于为什么会发生这种情况或我如何控制这种行为的任何建议?

这应该在文档中 -

Serializable属性必须是基元类型(例如,字符串、整数、双精度、布尔值或包含这些类型的映射或列表(。可序列化的值直接传递到从请求正文读取或写入响应正文的编解码器(默认情况下,此编解码器为 JSON(。对于自定义类型(如OtherThing(,编解码器不知道如何编码或解码该类型。

对于复杂类型,可以使用映射:

@Serialize()
Map<String, dynanic> get firstAndSecond() {
return {"first": first, "second": second};
}

您还可以使用类似 CSV 的数据:

@Serialize()
String get firstAndSecond() {
return "$first,$second";
}

最新更新