Home > other >  The method Save isn't defined for the class State ? || Flutter/dart
The method Save isn't defined for the class State ? || Flutter/dart

Time:12-16

Do you help solve this problem ? I have a problem save()

enter image description here

The method 'save' can't be unconditionally invoked because the receiver can be 'null'.

TextFormField(
  onSaved:(val){
    _authData['email'] = val.toString() ;
    print( _authData['email']);
  } ,

),
 ElevatedButton(onPressed: _submit, child: Text(_authMode == AuthMode.Login? 'Login' : 'Signup')),
//-------------------

void _submit() {
_formKey.currentState.save();

}

CodePudding user response:

 formKey.currentState?.save()

Add null check for state before calling save method

CodePudding user response:

Try to change below line ,add null check operator(!) refer null-safety here

_formKey.currentState.save();

To

_formKey.currentState!.save();

Or

_formKey.currentState?.save();

CodePudding user response:

There was a problem with the key

was before

GlobalKey _formKey = GlobalKey();
 

And that's wrong X

And the correct one is ✔✔

final GlobalKey<FormState> _formKey = GlobalKey();

  • Related