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

1,070

Here's the first;

(_formKey.currentState!.validate())

And the next;

 late Future<File> file;
Share:
1,070
Meggy
Author by

Meggy

I'm a self-taught novice stumbling like a drunk through php, javascript, mysql, drupal and flutter.

Updated on December 29, 2022

Comments

  • Meggy
    Meggy over 1 year

    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;
    
  • nvoigt
    nvoigt about 3 years
    That might be valid for your specific piece of code, but this is not a general rule. For someone else, with the exact snippet of code in another context than your program, it might be wrong.