value property
Returns the list of model objects.
If the underlying rawValue is null, this getter returns an empty
list [] as a default value. This ensures that list fields always
provide a valid list when accessed, preventing null reference errors.
Returns:
The list of T objects, or an empty list if rawValue is null.
Example:
final field = JsonList<UserModel>('users');
print(field.value); // [] (empty list, default)
field.value = [user1, user2];
print(field.value.length); // 2
Implementation
@override
List<T> get value => rawValue ?? [];
Sets the list value, accepting both typed lists and JSON arrays.
This setter provides flexible input handling:
List<T>: Assigned directly to rawValueList(untyped): Each element is deserialized into a new instance of typeTusing dependency injection (GetIt.instance.get<T>()) and then populated with data viafromJson()null: Sets rawValue to an empty list
Parameters:
value: The value to set, which can be aList<T>, an untypedListof JSON maps, ornull.
Example:
final field = JsonList<UserModel>('users');
// From typed list
field.value = [user1, user2];
// From JSON array
field.value = [
{'name': 'John', 'age': 30},
{'name': 'Jane', 'age': 25}
]; // Automatically creates UserModel instances
Implementation
@override
set value(dynamic value) {
if (value == null) {
rawValue = [];
return;
}
if (value is List<T>) {
rawValue = value;
return;
}
if (value is List) {
rawValue = value.map((element) {
final model = GetIt.instance.get<T>();
model.fromJson(element);
return model;
}).toList();
}
}