如何制作 public 属性 即使在飞镖中有下划线?

how to make a public property even with underscore in dart?

我正在从其余 api 服务器获取一些 json 数据,其中一个键是 _id,我需要将此 json 序列化为飞镖使用 built_value 的对象,但这是不允许的,因为在 dart 中 _id 是私有的,而 built_value 不允许我在我的模型中定义私有的 getter!
那我该怎么办?

package:built_value 有一种重命名字段的机制。正如其 README.md 中所述:

The corresponding dart class employing built_value might look like this. Note that it is using ... the @BuiltValueField annotation to map between the property name on the response and the name of the member variable in the Person class.

  ...

  @nullable
  @BuiltValueField(wireName: 'first_name')
  String get firstName;

因此,在您的情况下,您应该能够执行以下操作:

@BuiltValueField(wireName: '_id')
String get id;