Search packages, repositories and app categories

Package detailsv0.8.1

formz

A unified form representation in Dart which aims to simplify form representation and validation in a generic way.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
242K
Likes
919
Pub points
160

Install package

flutter pub add formz

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

Formz is a Dart package that provides a unified and generic approach to form representation and validation. It simplifies the management of form states by offering a consistent structure for handling inputs, validation rules, and error messages. Built with simplicity and reusability in mind, Formz enables developers to create forms with minimal boilerplate code while maintaining type safety and scalability across platforms. It integrates seamlessly into Flutter applications, supporting all major platforms including Android, iOS, Web, macOS, Windows, and Linux.

Use cases

  • Building reusable form components
  • Managing complex form validation logic
  • Creating consistent form state handling
  • Simplifying form submission workflows
  • Integrating with state management solutions
  • Supporting internationalized error messages

Key features

  • Generic form state representation
  • Built-in validation support
  • Extensible validation rules
  • Type-safe form fields
  • Minimal boilerplate
  • Cross-platform compatibility

Suitable for

  • Flutter developers seeking clean form patterns
  • Projects requiring robust form validation
  • Teams wanting reusable form logic
  • Apps targeting multiple platforms
  • Developers using functional or reactive state management

Considerations

  • Requires understanding of Dart generics
  • May need additional setup for custom validators
  • Best paired with state management like Bloc or Provider
  • Not a full UI solution—focuses on form logic
  • Limited built-in UI components

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

📝 Formz

Very Good Ventures Very Good Ventures

Developed with 💙 by Very Good Ventures 🦄

ci coverage pub package License: MIT style: very good analysis


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 errors
enum NameInputError { empty }

// Extend FormzInput and provide the input type and error type.
class NameInput extends FormzInput<String, NameInputError> {
  // Call super.pure to represent an unmodified form input.
  const NameInput.pure() : super.pure('');

  // Call super.dirty to represent a modified form input.
  const NameInput.dirty({String value = ''}) : super.dirty(value);

  // Override validator to handle validating a given input value.
  @override
  NameInputError? validator(String value) {
    return value.isEmpty ? NameInputError.empty : null;
  }
}

Interact with a FormzInput

const name = NameInput.pure();
print(name.value); // ''
print(name.isValid); // false
print(name.error); // NameInputError.empty
print(name.displayError); // null

const joe = NameInput.dirty(value: 'joe');
print(joe.value); // 'joe'
print(joe.isValid); // true
print(joe.error); // null
print(name.displayError); // null

Validate Multiple FormzInput Items

const validInputs = <FormzInput>[
  NameInput.dirty(value: 'jan'),
  NameInput.dirty(value: 'jen'),
  NameInput.dirty(value: 'joe'),
];

print(Formz.validate(validInputs)); // true

const invalidInputs = <FormzInput>[
  NameInput.dirty(),
  NameInput.dirty(),
  NameInput.dirty(),
];

print(Formz.validate(invalidInputs)); // false

Automatic Validation

class LoginForm with FormzMixin {
  LoginForm({
    this.username = const Username.pure(),
    this.password = const Password.pure(),
  });

  final Username username;
  final Password password;

  @override
  List<FormzInput> get inputs => [username, password];
}

void main() {
  print(LoginForm().isValid); // false
}

Caching validation results

For cases where the validator method has an expensive implementation, consider using the FormzInputErrorCacheMixin mixin to cache the error result and improve performance.

import 'package:formz/formz.dart';

enum EmailValidationError { invalid }

class Email extends FormzInput<String, EmailValidationError>
    with FormzInputErrorCacheMixin {
  Email.pure([super.value = '']) : super.pure();

  Email.dirty([super.value = '']) : super.dirty();

  static final _emailRegExp = RegExp(
    r'^[a-zA-Z\d.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z\d-]+(?:\.[a-zA-Z\d-]+)*$',
  );

  @override
  EmailValidationError? validator(String value) {
    return _emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;
  }
}

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package

Rankings