Open dotdoom opened 2 years ago
Would it work to add a finalize
hook to the builder?
https://pub.dev/documentation/built_value/latest/built_value/BuiltValueHook-class.html
It can fill in the key
... somehow. It's not pretty, but you could pass it via a private static field.
Partial deserialization with builders does seem a reasonable thing to support; will have a think about it next time I'm looking at features. That might be a while, though.
That definitely makes a workaround. For primitive types and modifiable snapshot.value
(e.g. a modifiable Map) which will be discarded after deserialization anyway, one can also add those artificial values to the snapshot before deserializing.
But indeed both ways look rather hacky.
Problem statement
Automatically deserialize objects with additional information inferred from context. E.g.: deserialize an entity from database, then immediately assign "key" property which is not in object's JSON but is known to the code calling deserializer:
Example
```dart // { ... // "42abef": { // "name": "Andreas" // } // } class Person implements BuiltMarking those extra fields nullable forces all uses to add a null check operator (
person.key!
).Proposal
Deserialize to a Builder, rather than the model itself, then have the library user's code assign missing field and call
.build()
:Alternatively, have the
deserialize
method take a Builder with some fields already assigned, then backfill it and ship back the ready model.