From 0a8946a7ce24fec5898600517c6511df131b50fb Mon Sep 17 00:00:00 2001 From: Anthony Sansone Date: Mon, 29 Jan 2024 19:49:52 -0600 Subject: [PATCH] Clarify GlobalKeys in Forms (#10094) Add reason for Forms to use StatefulWidgets.. Fixes #10063 --- src/cookbook/forms/validation.md | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/cookbook/forms/validation.md b/src/cookbook/forms/validation.md index 93f84f2016..59aeac5cfd 100644 --- a/src/cookbook/forms/validation.md +++ b/src/cookbook/forms/validation.md @@ -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()` 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. ```dart