I'm trying using built_value in flutter, and found if I declared a Type use built_value, I can commonly use dot syntax to assign value to it's properties: my declaration is:
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;
}
and use it like this:
Post p = Post();
p.titie = "hello world";
got error:
[dart] No setter named 'title' in class 'Post'.
I'm not familar the builder
thing, even I found out that the PostBuilder
have the setter of all properties:
PostBuilder().title = 'hello world';
but how can I use it?
BuiltValue classes are immutable. That's one of its main features.
Immutability means you can't modify an instance. Each modification has to result in a new instance.
One of several ways is
p = (p.toBuilder().titie = 'hello world').build();
to get an updated instance.
Alternatively
p = p.rebuild((b) => b..title = 'hello world');
In fact, there's a full example alone with source code.
// 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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With