value property
Returns the model object, creating a new instance if needed.
If the underlying rawValue is null, this getter creates and returns
a new instance of type T using dependency injection. This lazy
instantiation ensures that you always have a valid model object to work
with, even if the field hasn't been populated yet.
Returns:
The model object of type T. If rawValue is null, a new instance
is created via GetIt.instance.get<T>().
Example:
final field = JsonObject<AddressModel>('address');
final address = field.value; // Creates new AddressModel instance
address['city'] = 'New York';
Implementation
@override
T get value {
if (rawValue == null) {
return GetIt.instance.get<T>();
}
return rawValue!;
}
Sets the model object value, accepting both typed models and JSON maps.
This setter provides flexible input handling:
T(typed model): Assigned directly to rawValueMap<String, dynamic>: A new instance of typeTis created using dependency injection, then populated with data viafromJson()null: Sets rawValue tonull
Parameters:
value: The value to set, which can be a model instance of typeT, a JSON map, ornull.
Example:
final field = JsonObject<AddressModel>('address');
// From typed model
field.value = addressModel;
// From JSON map
field.value = {
'street': '123 Main St',
'city': 'New York'
}; // Automatically creates and populates AddressModel
Implementation
@override
set value(dynamic value) {
if (value == null) {
rawValue = null;
return;
}
if (value is T) {
rawValue = value;
return;
}
if (value is Map<String, dynamic>) {
final model = GetIt.instance.get<T>();
model.fromJson(value);
rawValue = model;
}
}