I have created a form by wrapping a column widget in Form()
as so
Form(
key: formKey,
child: Column(
children: [
But when I try to set final isValid = formKey.currentState!.validate();
to check, I get an exception has occured message _CastError (Null check operator used on a null value)
Any advice? Example Pic
CodePudding user response:
At that point the currentState is null, you are attempting to validate the form before it's built. It would make more sense to validate the form in a callback when some submit button is clicked, etc. Check Link to flutter docs for an example.
CodePudding user response:
you need to create function like this and call when some submit button is clicked, etc. Thanks.
void _saveForm() {
final isValid = formKey.currentState!.validate();
if (!isValid) {
// ignore: avoid_returning_null_for_void
return null;
}
_form.currentState!.save();
// .... extra code
}