(Flutter) Error: The argument type 'String?' can't be assigned to the parameter type 'String' because 'String?' is nullable and 'String' isn't

Viewed 218

im using flutter in android studio. coding as below.

class _loginscreenState extends State<loginscreen> {
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  String? _email, _password;

  void signin() async {
    await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: _email, password: _password)
        .catchError((onError) {
      print(onError);
    }).then((authUser) => {print(authUser.user?.uid)});
  }

please help me to solve this why the code cant run and what is the meaning of "'String?' is nullable and 'String' isn't". and what is the solution?

6 Answers
void signin() async { 
   await FirebaseAuth.instance .signInWithEmailAndPassword(
      email: _email! /* <-- */, 
      password: _password! /* <-- */).catchError((onError) {
        print(onError);
      }).then((authUser) => print(authUser.user?.uid));
}

try to add null(!)

class _loginscreenState extends State<loginscreen> {
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  String? _email, _password;

  void signin() async {
    await FirebaseAuth.instance
    //here change email -->email.tostring() and passsword--->password.tostring()
        .signInWithEmailAndPassword(email: _email.toString(), password: _password.toString())
        .catchError((onError) {
      print(onError);
    }).then((authUser) => {print(authUser.user?.uid)});
  }

Here you must supply email and password.if it was String?. may or maynot it contain null value .so Avoiding that email.tostring() we can use tostring() .if email is a null value it will converted to "Null" after adding email.toString() .

this is caused by using sound null safety in your app and you are passing a nullable string to a non-nullable string some solutions are:

  1. you can assert that the values for _email, _password are non-null strings
  2. or using the bang operator

for more elaboration on thisstring is null and string isnt or how to resolve argument type string...

You have made String? _email, _password; email and password nullable. after that you haven't initialized the value of email and password. and you are directly using email and password inside FirebaseAuth.instance.signInWithEmailAndPassword which does not support null values. Try initializing the value of email and password.

You can use TextEditingController if you are fetching value from textfield. and pass the value of email and password as parameter.

 TextEditingController emailController = TextEditingController();
  TextEditingController passwordController = TextEditingController();
Future<String> signUp(
      {required String email, required String password}) async {
  final FirebaseAuth _auth;


      await _auth
          .createUserWithEmailAndPassword(email: email, password: password);

Then use this function.

signUp(email: nameController.text,password: passwordController.text);

You can use The null assertion operator (!), but make sure that given values are not null.

 void signin() async { 
    await FirebaseAuth.instance .signInWithEmailAndPassword(
    email: _email!, password: _password!,
    ) .catchError((onError) { 
    print(onError); 
    }).then((authUser) => {print(authUser.user?.uid)}); 
    }

or you can check that values are not null before calling the method.

Like that

void signin() async { 
    if(_email!=null&&_password!=null){
         await FirebaseAuth.instance .signInWithEmailAndPassword(
         email: **_email!**, password: **_password!**,
         ) .catchError((onError) { 
         print(onError); 
         }).then((authUser) => {print(authUser.user?.uid)}); 
        }
    }
class _loginscreenState extends State<loginscreen> {
  GlobalKey<FormState> formKey = GlobalKey<FormState>();

  String _email, _password;

  void signin() async {
    await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: _email, password: _password)
        .catchError((onError).toString() {
      print(onError);
    }).then((authUser) => {print(authUser.user?.uid)});
  }
Related