1

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?

lava
  • 6,020
  • 2
  • 31
  • 28

5 Answers5

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

jamesdlin
  • 81,374
  • 13
  • 159
  • 204
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 01 '22 at 04:54
0
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() .

lava
  • 6,020
  • 2
  • 31
  • 28
0

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...

Asfemi
  • 3
  • 4
0

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);
Amarjeet Patidar
  • 496
  • 1
  • 4
  • 9
0
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)});
  }
Md Nezam Uddin
  • 262
  • 1
  • 5