Is there a solution to the error: The method 'validate' can't be unconditionally invoked because the receiver can be 'null'

Viewed 490

Is there a solution to the error: The method 'validate' can't be unconditionally invoked because the receiver can be 'null'

My Code

onPressed: () async {
                        if (_formKey.currentState.validate()) {
                          dynamic result = await _auth.signInWithEmailAndPassword(email, password);
                          if(result == null){
                            setState(() => error = 'Could not sign in with those credentials.');
                          }
                        }
                      }

formKey is a global key

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

try:

if (_formKey.currentState!.validate()) ...
Related