toJson method

  1. @override
List<Map<String, dynamic>>? toJson()
override

Serializes the list of model objects to a JSON array.

Each model in the list is serialized using its toJson() method, resulting in a list of JSON maps. Returns null if the list is null, allowing the field to be omitted from JSON output when appropriate.

Returns: A list of JSON maps, each representing a serialized model object, or null if the list is null.

Example:

final field = JsonList<UserModel>('users');
field.value = [user1, user2];
print(field.toJson());
// [{'name': 'John', 'age': 30}, {'name': 'Jane', 'age': 25}]

Implementation

@override
List<Map<String, dynamic>>? toJson() {
  return rawValue?.map((element) => element.toJson()).toList();
}