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.