get<T extends FormFieldState<G>, G> method

T get<T extends FormFieldState<G>, G>(
  1. String fieldName
)

Gets the state of a form field by its fieldName.

Throws an ArgumentError if no field with the specified fieldName is found. Provides a detailed error message in case of type mismatch, including a debugging tip.

Implementation

T get<T extends FormFieldState<G>, G>(String fieldName) {
  if (!fields.containsKey(fieldName)) {
    throw ArgumentError('No field found with name $fieldName');
  }
  final field = fields[fieldName];
  if (field is T) {
    return field;
  } else {
    throw ArgumentError('Type mismatch for field "$fieldName".\n\n'
        'Expected: $T\n\n'
        'Actual: ${field.runtimeType}\n\n'
        'Debugging Tip:\n\n'
        'If you have declared the field as ${field.runtimeType}, make sure to specify the generic type.\n\n'
        'For example, if the actual type is "RadioGroupFieldState<dynamic>", you might need to change it to '
        '"RadioGroupFieldState<bool>" in the field definition.\n\n');
  }
}