Flutter/Dart Null Safety on _formKey.currentState.validate()?

Issue

I’ve just updated my minimum sdk in pubspec.yaml;

 sdk: ">=2.12.0 <3.0.0" 

Now I have to change all my variables by declaring ? on all Strings and integers.

But what shall I do with the formkey?

 if (_formKey.currentState.validate())

Here’s what Android Studio tells me but I’m not sure where to put the ?

The method 'validate' can't be unconditionally invoked because the receiver can be 'null'.  Try making the call conditional (using '?.') or adding a null check to the target ('!').

Any idea how I should convert this line to null safety?

And how about this?

 Future<File> file;

Solution

Here’s the first;

(_formKey.currentState!.validate())

And the next;

 late Future<File> file;

Answered By – Meggy

Answer Checked By – Katrina (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *