value property

  1. @override
T get value
override

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!;
}
  1. @override
set value (dynamic value)
override

Sets the model object value, accepting both typed models and JSON maps.

This setter provides flexible input handling:

  • T (typed model): Assigned directly to rawValue
  • Map<String, dynamic>: A new instance of type T is created using dependency injection, then populated with data via fromJson()
  • null: Sets rawValue to null

Parameters:

  • value: The value to set, which can be a model instance of type T, a JSON map, or null.

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;
  }
}