Skip to content

Commit

Permalink
Clarify GlobalKeys in Forms (#10094)
Browse files Browse the repository at this point in the history
Add reason for Forms to use StatefulWidgets.. Fixes #10063
  • Loading branch information
atsansone authored Jan 30, 2024
1 parent b2dbf1a commit 0a8946a
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/cookbook/forms/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ a single text field using the following steps:

## 1. Create a `Form` with a `GlobalKey`

First, create a [`Form`][].
The `Form` widget acts as a container for grouping
and validating multiple form fields.
Create a [`Form`][].
The `Form` widget acts as a container for grouping and
validating multiple form fields.

When creating the form, provide a [`GlobalKey`][].
This uniquely identifies the `Form`,
and allows validation of the form in a later step.
This assigns a unique identifier to your `Form`.
It also allows you to validate the form later.

Create the form as a `StatefulWidget`.
This allows you to create a unique `GlobalKey<FormState>()` once.
You can then store it as a variable and access it at different points.

If you made this a `StatelessWidget`, you'd need to store this key *somewhere*.
As it is resource expensive, you wouldn't want to generate a new
`GlobalKey` each time you run the `build` method.

<?code-excerpt "lib/form.dart"?>
```dart
Expand Down

0 comments on commit 0a8946a

Please sign in to comment.