A unified form representation in Dart.
Formz aims to simplify form representation and validation in a generic way.
Create a FormzInput
import'package:formz/formz.dart';
// Define input validation errorsenumNameInputError { empty }
// Extend FormzInput and provide the input type and error type.classNameInputextendsFormzInput<String, NameInputError> {
// Call super.pure to represent an unmodified form input.constNameInput.pure() : super.pure('');
// Call super.dirty to represent a modified form input.constNameInput.dirty({String value = ''}) : super.dirty(value);
// Override validator to handle validating a given input value.@overrideNameInputError? validator(String value) {
return value.isEmpty ? NameInputError.empty : null;
}
}
For cases where the validator method has an expensive implementation, consider using the FormzInputErrorCacheMixin mixin to cache the error result and improve performance.