Trying to fetch a document name "abcd".
await _firestore
.collection("users")
.where("name", isEqualTo: _auth.currentUser?.uid)
.get()
.then((value) {
setState(() {
userMap = value.docs[0].data();
});
});
.uid is not providing the document "abcd". How can I get it?
Only need the name of document.
That name "abcd" is the document id in your Firestore database, if you need to access it you will need to set the document id as its name which you look in the Firestore database :
await _firestore
.collection("users")
.doc("abcd")
.get() ; // this will get you the abcd document
and if you want to query your collection based on uid, and get the document information from it, first you will need a field called "name" which it's value id the user's uid, then :
await _firestore
.collection("users")
.where("name", isEqualTo: _auth.currentUser?.uid)
.get(querySnapshot).then(() {
final document = querySnapshot.docs.first;
print(document.id); // this will print abcd
});
Related
I want to get field value.
my code is..
void _checkNumner(String number) async {
final userRef = firestore.collection('users');
var documentSnapshot =
await userRef.where("number", isEqualTo: true).get().then((num) {
QuerySnapshot<Map<String, dynamic>> number = num;
print(number);
print("test");
});
print(documentSnapshot);
}
but my console is
how I get field number?
I want to load number values in all docs.
I'm so beginer. T.T
Please reply from the masters
Thank you
Firebase firestore is a NoSQL, document-oriented database. User Data is stored in documents which are organized into collection , i.e collection contains list of document. In simpler words we can say QuerySnapshot contains/provide group of DocumentSnapshot. more about firestore data model
Collection --> QuerySnapshot --> Group of DocumentSnapshot
Document --> DocumentSnapshot
1) Fetch from collection - QuerySnapshot
Here we'll get list of DocumentSnapshots, we can filter by using where commad
Future<void> checkNumber(int number) async {
final QuerySnapshot snapshot = await FirebaseFirestore.instance
.collection('users')
.where("number", isEqualTo: number)
.get();
snapshot.docs.isEmpty
? {
//TODO: your code here
debugPrint("no data found")
}
: {
for (DocumentSnapshot element in snapshot.docs)
{
//TODO: your code here
debugPrint("number is: ${element['number']}"),
debugPrint("name is: ${element['name']}"),
}
};
}
1) Fetch from document - DocumentSnapshot
To fetch data from document we require documentId, and we get a single documentSnapshot instead of multiple like in above way.
Future<void> checkNumberWithDocId() async {
const String docId = 'aaaa';
final DocumentSnapshot snapshot = await FirebaseFirestore.instance.collection('users').doc(docId).get();
snapshot.exists
? {
//TODO: your code here
debugPrint("no data found")
}
: {
//TODO: your code here
debugPrint("number is: ${snapshot['number']}"),
debugPrint("name is: ${snapshot['name']}"),
};
}
I am trying to get a document from the database where the users can put in an email and it will pull the document and save it so I can use the document in an editing module. Here is my readUsers method
Future ReadUserinDatabase(String email) async {
Stream<List<DatabaseUser>> User = FirebaseFirestore.instance
.collection('Users')
.where('Email' == email)
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => DatabaseUser.fromJson(doc.data()))
.toList());
print(User.map);
}
I will be grabbing the document ID from the returned document so I can put it in for doc to edit
Future EditUserinDatabase(
String oldemail, String newemail, String oldrole, String newrole) async {
final docUser = FirebaseFirestore.instance.collection('TEST').doc('Doc ID goes Here');
await docUser.update({'Email': newemail, 'Role': newrole});
}
The error that is occurring is when I print User.map it is not printing the information in the document it is printing '$user' and is not saving in a form I can use it. How do I save the document
How can I reference the collection in the document I auto-id in Firebase?
final CollectionReference _olanaklar5 = _database
.collection("Kategoriler")
.doc("Hoteller")
.collection("5_Yıldızlı")
.doc() //======> here
.collection("Olanaklar");
You can get list all document id with doc.id
List<String> _listDocId = [];
await fireStore
.collection("Kategoriler")
.doc("Hoteller")
.collection("5_Yıldızlı")
.get()
.then((QuerySnapshot querySnapshot) {
for (var doc in querySnapshot.docs) {
_listDocId.add(doc.id);
}
});
and query in list doc id
for (var id in _listDocId) {
final CollectionReference _olanaklar5 = _database
.collection("Kategoriler")
.doc("Hoteller")
.collection("5_Yıldızlı")
.doc(id)
.collection("Olanaklar");
}
If you're using Firebase auth, it's preferred to keep User's UID as doc, else you can use .where() as a query to match the fields in all documents. But as the app scales, it will be a hectic process and will consume many reads.
I have a collection named requests and inside that collection I have many Document. I want to get the Id of each document present inside that collection
Firestore.instance
.collection(FirebaseCollection.user)
.where(UserCollectionField.mobile, isEqualTo: mobile)
.getDocuments()
.then((snapshot) {
var id = snapshot.documents[0].documentID;
}).catchError((error) {
});
also you can make list of user id ex.
Firestore.instance
.collection(FirebaseCollection.user)
.where(UserCollectionField.mobile, isEqualTo: mobile)
.getDocuments()
.then((querySnapshot) async {
var list = querySnapshot.documents;
list.forEach((document) {
adminlist.add(document.documentID);
});
});
In Dart, how do you retrieve the auto-generated ID within a collection's document?
I have a collection called "users" that has auto ID documents with the users details. How do you retrieve that ID?
ID eg: yyHYmbDelykMPDWXHJaV
I'm trying to get this id. When the document is first created, when user is first created, the user.uid is stored in it's collection.
I can retrieve all the data from the users database, which I don't want:
void getData() {
databaseReference
.collection('users')
.getDocuments()
.then((QuerySnapshot snapshot) {
snapshot.documents.forEach((f) => print('${f.data}'));
});
}
I can get a future reference for the current user's user.uid but that still doesn't give me the unique document ID.
Future<DocumentReference> getUserDoc() async {
final FirebaseAuth _auth = FirebaseAuth.instance;
final Firestore _firestore = Firestore.instance;
FirebaseUser user = await _auth.currentUser();
DocumentReference ref =
_firestore.collection('users').document(user.uid);
print(ref);
return ref;
}
Is it possible to search the user.uid against the database users and retrieve the ID that way?
To get the auto generated id, you can just call the document method on whatever collection you're trying to get a document from and the returned value will have a getter for documentID
final userDocument = usersCollection.document();
final documentID = userDocument.documentID;
You usually do this when you first want to create a document. To retrieve the documentID after you've created it, I'd add it to the user document itself:
userDocument.setData({
documentID: documentID,
/* ... */
});
Instead of using the auto generated documentID though, I personally use the firebase user's uid property just because it ties it back to Firebase auth
// user is a FirebaseUser
final userDocument = usersCollection.document(user.uid);
userDocument.setData({
uid: user.uid,
// ...displayName, email, etc.
});