iJamesPHP2
iJamesPHP2

Reputation: 534

GlobalKey<FormState> has no validate() method?

I am defining the key for my form like so:

final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

I am passing it to my form like so:

Form(key: _formKey)

However, when I try to access _formKey.validate() I get the following error:

 lib/screens/checklists/AccidentChecklist.dart:341:54: Error: The method 'validate' isn't defined for the class 'GlobalKey<FormState>'.
 - 'GlobalKey' is from 'package:flutter/src/widgets/framework.dart' ('../flutter/packages/flutter/lib/src/widgets/framework.dart').
 - 'FormState' is from 'package:flutter/src/widgets/form.dart' ('../flutter/packages/flutter/lib/src/widgets/form.dart').
Try correcting the name to the name of an existing method, or defining a method named 'validate'.
            _nextStepButton(_formKey.validate())   

I really have no clue why I'm receiving this error because as we all know, this method exists, I'm quite experienced with Flutter and wouldn't be asking this question unless I was truly stumped, any suggestions?

Flutter version info:

Flutter 1.24.0-4.0.pre.107 • channel master • https://github.com/flutter/flutter.git Framework • revision 35a94f70e1 (5 months ago) • 2020-10-27 23:47:04 -0700 Engine • revision 64e6599910 Tools • Dart 2.11.0 (build 2.11.0-260.0.dev)

Upvotes: 1

Views: 1265

Answers (2)

Kingshuk Saha
Kingshuk Saha

Reputation: 154

Add Null Check to the validate method

formKey.currentState!.validate()

Upvotes: 1

Siddharth Agrawal
Siddharth Agrawal

Reputation: 3146

The command is

_formKey.currentState.validate();

not

_formKey.validate()

Upvotes: 4

Related Questions