I Have a form Key But Flutter Still Gives Me An Error When Running Validate

1,649 views
Skip to first unread message

Mr harilall

unread,
Feb 20, 2020, 4:10:17 AM2/20/20
to Flutter Development (flutter-dev)
Hi,

I have created a form key
final _formKey = GlobalKey<FormState>();

I have also assigned this key to my form
Form( key: _formKey)

Then in my Raised Button's onPressed method I have this code to disable the button if the form is invalid
onPressed: (_formKey.currentState.validate() || _photoCount == 0) ? null : () {},
Now as far as I know this code is only supposed to run once the button is actually pressed but it seems to fire once the view is loaded somehow and it gives me the following error
NoSuchMethodError: The method 'validate' was called on null. Receiver: null Tried calling: validate()

I have also been through every text and number input and made sure they all have validator functions but that didn't work. How do I resolve this?

Olaide Nojeem Ekeolere

unread,
Feb 20, 2020, 4:13:47 AM2/20/20
to Mr harilall, Flutter Development (flutter-dev)
Hi,
    Because the onPressed is being fired immediately the widget builds modify as: onPressed: ()=> (_formKey.currentState.validate() || _photoCount == 0) ? null : () {},
Best regards
Olaide Nojeem Ekeolere

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/9547bf62-bf65-4d5a-8e9a-a076d030e650%40googlegroups.com.

Mr harilall

unread,
Feb 20, 2020, 4:49:43 AM2/20/20
to Flutter Development (flutter-dev)
Ok so how do I rectify this?


On Thursday, February 20, 2020 at 11:13:47 AM UTC+2, Olaide Nojeem Ekeolere wrote:
Hi,
    Because the onPressed is being fired immediately the widget builds modify as: onPressed: ()=> (_formKey.currentState.validate() || _photoCount == 0) ? null : () {},
Best regards
Olaide Nojeem Ekeolere
On Thu, 20 Feb 2020 at 10:10, Mr harilall <harila...@gmail.com> wrote:
Hi,

I have created a form key
final _formKey = GlobalKey<FormState>();

I have also assigned this key to my form
Form( key: _formKey)

Then in my Raised Button's onPressed method I have this code to disable the button if the form is invalid
onPressed: (_formKey.currentState.validate() || _photoCount == 0) ? null : () {},
Now as far as I know this code is only supposed to run once the button is actually pressed but it seems to fire once the view is loaded somehow and it gives me the following error
NoSuchMethodError: The method 'validate' was called on null. Receiver: null Tried calling: validate()

I have also been through every text and number input and made sure they all have validator functions but that didn't work. How do I resolve this?

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutt...@googlegroups.com.

Olaide Nojeem Ekeolere

unread,
Feb 20, 2020, 4:57:16 AM2/20/20
to Mr harilall, Flutter Development (flutter-dev)
Hi,
Try   onPressed: ((_formKey.currentState!=null&&!_formKey.currentState.validate()) || _photoCount == 0) ? null : () {},


To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/c22f8be0-ec38-406f-a486-b17a44e7df22%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages