Why use Reactive Forms in Flutter?

Joan Pablo Jiménez
Flutter Community
4 min readJul 21, 2020

--

Haven’t you ever felt that getting data from a Form, validating a field, or even disabling the submit button is tedious and full of boilerplate code?

Fortunately, that’s why Reactive Forms exists.

1. Ease and clean way of collecting data from your Forms.

No more onSave, onChanges, onEditionCompleted callbacks needed or TextEditionControllers to collect data from inputs.

Reactive Forms get a clean separation between views and models and maintains data synchronization in a transparent two-way binding mechanism. Just declare your model and define your widgets. Then lay back and relax, data will flow smoothly.

2. Transparent Validations of inputs fields.

No more StatefulWidget defining a key for the Form widget, goodbye to Form.of(context) and Goodbye to:

Reactive Forms Validations occurs transparently. Just declare your model and define the validators you need. It will also handle the validity of the entire Form and will show error messages when needed. All of that without you needing to write a single line of imperative code.

3. Enable/Disable buttons depending on Form validity.

Getting this behavior, even in such a great framework as Flutter, can sometimes be hard and full of boilerplate code.

In Reactive Forms is as simple as asking to the Form if it is valid or not:

4. Perfect integration with Provider plugin and other state management plugins.

The use of a state management library (MVC, MVVM, or just BLoc) clean your code and perfectly separates responsibilities between the UI and business logic.

5. Focus/Unfocus text fields.

To add or remove focus to a control use FormControl.focus() or FormControl.unfocus(). No more StatefulWidgets and FocusNode declarations:

What is Reactive Forms?

  • Reactive Forms provides a model-driven approach to handling form inputs whose values change over time. It’s heavily inspired in Angular Reactive Form.
  • It lets you focus on business logic and save you time from collect, validate, and maintain synchronization data between models and widgets.
  • Remove boilerplate code and brings you the possibility to write clean code with minimal efforts.
  • Integrates perfectly with common state management libraries like Provider, Bloc, and many others.

What is not Reactive Forms?

  • Reactive Forms is not a fancy widget’s package. It’s not a widget’s library with new shapes, colors, or animations. It frees you from the responsibility of gathering and validating the data. And keeps the data in sync between your model and your widgets.
  • Reactive Forms doesn’t replace native widgets that you commonly use in Flutter like TextFormField, DropdownButtonFormField, or CheckboxListTile. It brings new two-way binding capabilities and many more features to those same widgets.

Reactive Forms is much more. This POST was just a preview of some basic features. Please visit Github Repo or plugin page in pub.dev for the full documentation.

https://www.twitter.com/FlutterComm

--

--