operator []= method

void operator []=(
  1. String name,
  2. dynamic value
)

Sets the value of a field by its name using bracket notation.

This operator provides convenient assignment to field values. The value will be automatically converted according to the field's type (e.g., strings can be parsed to numbers for numeric fields).

Parameters:

  • name: The name of the field to set (must match a field's JsonField.fieldName).
  • value: The new value to assign. The type and conversion behavior depend on the specific field type (see individual field documentation).

Throws: An Exception if no field with the given name exists in this model.

Example:

user['name'] = 'Jane Doe';
user['age'] = 25;
user['age'] = '30'; // String will be parsed to int

Implementation

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