built_value 对象的 setter 是什么

What's the setter of a built_value's object

我尝试在 flutter 中使用 built_value,发现如果我声明类型使用 built_value,我通常可以使用点语法为其属性赋值: 我的声明是:

abstract class Post implements Built<Post, PostBuilder> {
    Post._();
    int get userId;
    int get id;
    String get title;
    String get body;
    factory Post([updates(PostBuilder b)]) = _$Post;
    static Serializer<Post> get serializer => _$postSerializer;
}

并像这样使用它:

Post p = Post();
p.titie = "hello world";

出现错误:

[dart] No setter named 'title' in class 'Post'.

我不熟悉 builder 东西,即使我发现 PostBuilder 具有所有属性的 setter: PostBuilder().title = 'hello world'; 但我该如何使用它呢?

BuiltValue 类 是不可变的。这是它的主要特点之一。
不变性意味着您无法修改实例。每次修改都必须产生一个新实例。

其中一种方法是

p = (p.toBuilder().titie = 'hello world').build();

获取更新的实例。

或者

p = p.rebuild((b) => b..title = 'hello world');

事实上,有一个full example单独的源代码。

// Values must be created with all required fields.
final value = new SimpleValue((b) => b..anInt = 3);

// Nullable fields will default to null if not set.
final value2 = new SimpleValue((b) => b
..anInt = 3
..aString = 'three');

// All values implement operator==, hashCode and toString.
assert(value != value2);

// Values based on existing values are created via "rebuild".
final value3 = value.rebuild((b) => b..aString = 'three');
assert(value2 == value3);

// Or, you can convert to a builder and hold the builder for a while.
final builder = value3.toBuilder();
builder.anInt = 4;
final value4 = builder.build();
assert(value3 != value4);