1

i am trying to return value in users where i am getting this error if i am trying to fix this error by adding null sign it does not return any value becuase all this value is going to be null and i want to return this value and save it so i can use it what can be the solution Please guide

FirebaseAuth auth = FirebaseAuth.instance;
User? user = FirebaseAuth.instance.currentUser;
CollectionReference users = FirebaseFirestore.instance.collection('users');

Future<void> addUser(context) {
  //before we add user we need to check this user already signedin earlier using same number or not
  //if signedIn before no need to add , because it may have added already. so just need to skip adduser




  return users
      .add({
    'uid': user.uid,
    'mobile':  user.phoneNumber,
    'email': user.email
  })
      .then((value){
        //after add data to firebase then will go to next screen
    //if you pushreplacementName or push replacement, then u cant go back to previous screen
    Navigator.pushReplacementNamed(context, LocationScreen.id);

  })
      .catchError((error) => print("Failed to add user: $error"));
}
Amazee21
  • 11
  • 2
  • I doubt there is someone who will understand what you want to achieve. Edit your question, so people can understand you. – Mises Dec 26 '22 at 23:05
  • You "user" variable is nullable. It means that you have to check if "user" is null before accessing its properties (uid, phoneNumber...) – Octet Dec 26 '22 at 23:32
  • @Octet yes , it is nullable, if i removed it will throw nullable exception. Is there another way to resolve it becuase i am not getting my data back to firestore. It's showing null values in firestore. and i dont know why. i want to return value of users saved in it on runtime – Amazee21 Dec 27 '22 at 10:52
  • It just means that the user is not logged in when you call addUser(). That's why you have to check inside addUser() is user is null or not – Octet Dec 28 '22 at 00:04
  • @Octet yes you right it's not logged in but what i was trying to do is pass these values in those variables by retuning it but i resolved it by simply declaring it first seprately and then used it later let me show you how `User? user` **simply delcaring it seprately** `user = FirebaseAuth.instance.currentUser;` **and used it seprately** – Amazee21 Dec 29 '22 at 17:48

1 Answers1

0

that is because uid is nullable. try this

 return users
      .add({
    'uid': user.uid ?? "default username"
    'mobile':  user.phoneNumber,
    'email': user.email
  })

default username will be saved in firestore if uid is null.

Wai Yan Min Min
  • 153
  • 1
  • 7
  • yes you are right it's returning "default username" but i want to return the user values which are written on runtime and want to return to the firestore. – Amazee21 Dec 27 '22 at 10:49
  • it seems like you need to make sure you have stored the user input value in the user.uid variable before storing in firestore. can you provide me the code where you ask for the user input – Wai Yan Min Min Dec 28 '22 at 11:33
  • yes you are right but it was not storing it and was giving error i dont know why but i resolved it by simply declaring is it first seprately and then used it later let me show you how `User? user` **simply delcaring it seprately** `user = FirebaseAuth.instance.currentUser;` **and used it seprately** – Amazee21 Dec 29 '22 at 17:42