0

I am trying to put user info to Firestore Database, but it this is not working as I expected.

I installed required packages including cloud_firestore.

I used this code for adding users data to database.

try {
  setState(() {
    _isEntering = true;
  });
  if (_isLogin) {
    var userCredentials = await _firebase.signInWithEmailAndPassword(
        email: _enteredEmail, password: _enteredPassword);
  } else {
    var userCredentials = await _firebase.createUserWithEmailAndPassword(
        email: _enteredEmail, password: _enteredPassword);
    var storageRef = FirebaseStorage.instance
        .ref()
        .child("profile_images")
        .child("${userCredentials.user!.uid}.jpg");
    storageRef.putFile(_selectedImage!);
    var profileImageUrl = await storageRef.getDownloadURL();

    FirebaseFirestore.instance.collection("users").doc(userCredentials.user!.uid).set({
       "username":"will be given..",
       "email":_enteredEmail,
       "profile_image_url":profileImageUrl
     });


  }
} on FirebaseAuthException catch (e) {
  if (e.code == "email-already-in-use") {}
  ScaffoldMessenger.of(context).clearSnackBars();
  ScaffoldMessenger.of(context).showSnackBar(
    SnackBar(
      content: Text(e.message ?? "Authentication failed"),
    ),
  );
  setState(() {
    _isEntering = false;
  });
}

Also, I tried to use the same approach as said in official Firestore wesite. text

    var db = FirebaseFirestore.instance;


    final user = <String, dynamic>{
      "username":"will be given..",
       "email":_enteredEmail,
       "profile_image_url":profileImageUrl
    };

    // Add a new document with a generated ID
    db.collection("users").add(user).then((DocumentReference doc) =>
        print('DocumentSnapshot added with ID: ${doc.id}'));
  }

I am not sure what is causing to this problem. I have looked some questions which have partially same problems. enter link description here But again the reason is still unknown. If anyone knows something please help to make it work.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441

1 Answers1

0

Finally, I discovered the issue. In my project, when I was adding my profile image to Firebase Storage, I realized that I forgot to include the await keyword before the putFile operation. This mistake prevented the code from properly uploading the image to the storage.

    var userCredentials = await _firebase.createUserWithEmailAndPassword(
                email: _enteredEmail, password: _enteredPassword);

    var storageRef = FirebaseStorage.instance
        .ref()
        .child("profile_images")
        .child("${userCredentials.user!.uid}.jpg");
    storageRef.putFile(_selectedImage!);

    var profileImageUrl = await storageRef.getDownloadURL();

Updated code: var userCredentials = await _firebase.createUserWithEmailAndPassword( email: _enteredEmail, password: _enteredPassword);

    var storageRef = FirebaseStorage.instance
        .ref()
        .child("profile_images")
        .child("${userCredentials.user!.uid}.jpg");
    await storageRef.putFile(_selectedImage!);

    var profileImageUrl = await storageRef.getDownloadURL();

By using await when calling putFile, the code will wait for the upload operation to complete before moving to the next line.