Repository navigation
FormField widgets validated on every build() #36154
Copy link
Copy link
Closed
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsc: performanceRelates to speed or footprint issues (see "perf:" labels)Relates to speed or footprint issues (see "perf:" labels)frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.
Description
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:
- FormField immediately shows error on widget create.
- Validates and shows errors on all
Formchilds 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
a: text inputEntering text in a text field or keyboard related problemsEntering text in a text field or keyboard related problemsc: performanceRelates to speed or footprint issues (see "perf:" labels)Relates to speed or footprint issues (see "perf:" labels)frameworkflutter/packages/flutter repository. See also f: labels.flutter/packages/flutter repository. See also f: labels.
Activity
BondarenkoStas commentedon Sep 29, 2019
@audkar Is it the same using current flutter stable?
hauru commentedon Oct 23, 2019
Just wanted to confirm the aforementioned behavior on Flutter 1.10.14. This has an additional drawback, aside from problems mentioned by @audkar : with
autovalidateset to true, putting asetStatecall inside the validator generates an exception as the state gets modified during the build.StephenBrough commentedon Jan 13, 2020
Just saw the issue described by @hauru. Wrapping
setStatein anaddPostFrameCallbackworks as a temporary workaround, but the widget is still continuously built over and over.github-actions commentedon Aug 18, 2021
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 -vand a minimal reproduction of the issue.