skip to Main Content

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?

5

Answers


  1. 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(!)

    Login or Signup to reply.
  2. 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() .

    Login or Signup to reply.
  3. 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…

    Login or Signup to reply.
  4. 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);
    
    Login or Signup to reply.
  5. 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)});
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search