Skip to content

FormField widgets validated on every build() #36154

Closed

Description

@audkar

When FormField has autovalidate = true validator gets called on every widget build. My initial expectation was that it should happen only after value change. Documentation states similar case on final bool autovalidate;:

If true, form fields will validate and update their error text immediately after every change. Otherwise, you must call [FormState.validate] to validate.

This causes two problems:

  1. FormField immediately shows error on widget create.
  2. Validates and shows errors on all Form childs after first child widget value updated. Expectation is that only first Field gets validated which value got changed.

Steps to Reproduce

Validation error shown immediately on initial app start:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Text('Problem1 (immediate error)'),
            TextFormField(
              validator: (value) => "error",
              autovalidate: true,
            ),
            Text('Problem2 (everything validated after first Field change):'),
            Form(
              autovalidate: true,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    validator: (value) => "error",
                  ),
                  TextFormField(
                    validator: (value) => "error",
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Logs

[✓] Flutter (Channel stable, v1.7.8+hotfix.3, on Linux, locale en_US.UTF-8)
    • Flutter version 1.7.8+hotfix.3 at /.../flutter
    • Framework revision b712a172f9 (5 days ago), 2019-07-09 13:14:38 -0700
    • Engine revision 54ad777fd2
    • Dart version 2.4.0

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
    • Android SDK at /.../android-sdk
    • Android NDK location not configured (optional; useful for native profiling
      support)
    • Platform android-29, build-tools 29.0.1
    • ANDROID_HOME = /.../android-sdk
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_222-b05)
    • All Android licenses accepted.

Activity

  1. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Sep 29, 2019
  2. BondarenkoStas commented on Sep 29, 2019

    @BondarenkoStas

    @audkar Is it the same using current flutter stable?

  3. hauru commented on Oct 23, 2019

    @hauru

    Just wanted to confirm the aforementioned behavior on Flutter 1.10.14. This has an additional drawback, aside from problems mentioned by @audkar : with autovalidate set to true, putting a setState call inside the validator generates an exception as the state gets modified during the build.

  4. StephenBrough commented on Jan 13, 2020

    @StephenBrough

    Just saw the issue described by @hauru. Wrapping setState in an addPostFrameCallback works as a temporary workaround, but the widget is still continuously built over and over.

  5. added
    a: text inputEntering text in a text field or keyboard related problems
    c: performanceRelates to speed or footprint issues (see "perf:" labels)
    on Feb 26, 2020
  6. github-actions commented on Aug 18, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  7. locked as resolved and limited conversation to collaborators on Aug 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsc: performanceRelates to speed or footprint issues (see "perf:" labels)frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions