operator [] method

dynamic operator [](
  1. String name
)

Retrieves the value of a nested field within the model object.

Provides convenient access to fields within the nested model using bracket notation. This delegates to the model's [] operator.

Parameters:

  • name: The name of the nested field to retrieve (must match a field's JsonField.fieldName in the nested model).

Returns: The value of the nested field, or null if the model object is null.

Throws: An Exception if the nested model exists but no field with the given name exists.

Example:

final field = JsonObject<AddressModel>('address');
field.value = addressModel;
print(field['city']); // 'New York'

Implementation

operator [](String name) {
  if (rawValue == null) {
    return null;
  }
  for (final field in rawValue!.fields) {
    if (field.fieldName == name) {
      return field.value;
    }
  }
  throw Exception("Field $name does not exist");
}