I am getting argument type not assignable error in dart while checking the email value is empty or not the error which I am getting are

The argument type void Function(String) can't be assigned to the parameter type void Function(String?)?.

and

The argument type String? Function(String) can't be assigned to the parameter type 'String? Function(String?)?.

These are the validator classes,

class EmailFieldValidator {
static String? validate(String value) {
    return value.isEmpty ? 'Email can\'t be empty' : null;
  }

}

class PasswordFieldValidator {
  static String? validate(String value) {
    return value.isEmpty ? 'Password can\'t be empty' : null;
  }
}

Here is the code giving the error

 List<Widget> buildInputs() {
    return <Widget>[
      TextFormField(
        key: Key('email'),
        decoration: InputDecoration(labelText: 'Email'),
        validator: EmailFieldValidator.validate, //this gives string error mentioned above
        onSaved: (String value) => _email = value, //getting void error mention above 
      ),
      TextFormField(
        key: Key('password'),
        decoration: InputDecoration(labelText: 'Password'),
        obscureText: true,
        validator: PasswordFieldValidator.validate,  // getting string error mentioned above
        onSaved: (String value) => _password = value, //getting void error mention above 
      ),
    ];
  }


Solution 1: stacktrace2234

The problem that you might return a null value whil it does not let you to use null there.

Try to add ! after value:

onSaved: (String value) => _email = value!, 
onSaved: (String value) => _password = value!, 


Solution 2: Kushal agrawal

This is a problem faced after the Null Safety thing came to flutter. I just normally cast it as a String to use it. Sometimes it gives out errors but not errors which will potentially break your app.

onSaved: (String? value) => _email = value as String,

static String? validate(String? value) {
String valueString = value as String;
// Do Your Usual thing over here like checking if it's empty}


Solution 3: Alexander

First error

Both EmailFieldValidator.validate and PasswordFieldValidator.validate have the same type: String? Function(String). That is to say, they're both functions that take a non-optional String as a parameter, and return a optional String.

The error message is telling you that the validator parameter to TextFormField is expecting a function with the type String? Function(String?)?. That is to say, a optional function (that is, you can pass null if you don't need any validator functionality at all), which itself accepts a optional String parameter, and returns a optional String.

Notice the difference: The parameters of your validator functions are not optional, but they're required to be optional. Thus, you'll need to change their function signatures:

static String? validate(String? /* ⬅️ Notice this is now optional! */ value) { ... }

Though one might ask: why not just use a lambda here, and have the types be inferred?

Second error

The error is similar. Looking at the docs of TextFormField's constructor, we see that the type of the onSaved parameter is FormFieldSetter<String>, where FormFieldSetter is just a typealias for void Function(T? newValue). Thus, the resolved type of the onSaved parameter is void Function(String? newValue).

The lambda function you passed, (String value) => _password = value, takes a non-optional String parameter. Notice the mismatch. It needs to accept an optional, because the saving process might yield a null value, and you'll need to handle that.

How exactly you handle that null is up to you, and depends on your requirements. You might want to end up saving the null in your database (for example) if the field isn't mandatory. Or you might substitute a default value (though that doesn't make sense in this context). Or you in the extreme case, if you're absolutely sure you'll never ever get a null, you could force unwrap it. Of course, doing is as if you're saying the following to Dart:

I'm super-duper sure this will never be null. If it is, the sky is falling, and I haven't devised a way to deal with that yet. Things are clearly not working the way I expected. Rather than carrying on forward in this misbehaved state, please just crash my app and prevent further damage

Whether that's desirable or not, depends on your problem domain.