how to get collection list from document Id in flutter Firestore? [duplicate] - firebase

This question already has answers here:
Flutter & Firebase: Return list of collections
(2 answers)
Fetching all collections in Firestore
(3 answers)
How to list subcollection on firestore? [duplicate]
(1 answer)
Closed 1 year ago.
i want "a9XvvHPEvhfDWTXe8HqPqWuP2gg1" this documentId collection list.
FirebaseFirestore.instance
.collection('tbl_FriendList').doc(
'a9XvvHPEvhfDWTXe8HqPqWuP2gg1').get().then((event) {});

You can't do that with the Flutter sdk. You'll need to restructure your data.
Retrieving a list of collections is not possible with the mobile/web
client libraries. You should only look up collection names as part of
administrative tasks in trusted server environments. If you find that
you need this capability in the mobile/web client libraries, consider
restructuring your data so that subcollection names are predictable.
https://firebase.google.com/docs/firestore/query-data/get-data#list_subcollections_of_a_document

As per you image, seems like there is no document in this ID. You only have sub-collections.
To get sub-collection you have to improve your query and you should know your sub-collection name as mentioned below
await FirebaseFirestore.instance
.collection('tbl_FriendList')
.doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
.collection('collectionPath')
.get();
Or you have to store data in db like this
a9XvvHPEvhfDWTXe8HqPqWuP2gg1 > collectionName > documents
FirebaseFirestore.instance
.collection('tbl_FriendList')
.doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
.collection('collectionName')
.add({'data': 'data123'});
after that you can get data as mentioned below
FirebaseFirestore.instance
.collection('tbl_FriendList')
.doc('a9XvvHPEvhfDWTXe8HqPqWuP2gg1')
.collection('collectionName')
.get()
.then((value) => {});
This query will return all data you have in collectionName sub-collection.

Related

Flutter: Better way to update many documents in a single call [duplicate]

This question already has answers here:
Can Firestore update multiple documents matching a condition, using one query?
(8 answers)
Firestore update all documents in collections
(6 answers)
How to do a bulk update in Firestore
(4 answers)
Closed 9 months ago.
I have a function that allows a user to change their username, simply using firebase update({}). Every time a user sends a message in my chat page, it saves it as a documents on firebase with related data, user name, time message was sent etc.
The function below is for a chat page, which works how I want it to. it takes whatever the current users username and updates all past messages with the users new current username. I want to know if their is a better way to achieve this.
How can I change a single value in any amount of documents, which obviously have different ids, in one single call?
List<String> testList = []; //<--- List of document IDs of previous messages user has sent
FirebaseFirestore.instance
.collection('users')
.doc(loggedInUser.uid)
.update({'messageID': testList});
for (var item in testList) {
var collection = FirebaseFirestore.instance
.collection("chatrooms")
.doc(chatroom)
.collection("users")
.doc('ggg')
.collection("userMessage");
collection
.doc(item) // <-- Doc ID where data should be updated.
.update({'username': loggedInUser.userName.toString()}) // <-- Updated data
.then((_) => print('Updated'))
.catchError((error) => print('Update failed: $error'));

Is my Sub Collection is Also fetched when Fetching the Collection? [duplicate]

This question already has an answer here:
When fetching document from Firestore, am I also getting the collections inside this document?
(1 answer)
Closed 1 year ago.
I have a subcollection "Ratings" inside this "Posts" collection so when i get any post will this collection will be fetched automatically or i have to reference it separately. And if this subcollection Ratings is fetched with the post so it'll be count as a separate read or same as the Post read?
final CollectionReference postsCollection =
FirebaseFirestore.instance.collection("posts");
response = await FirebaseFirestore.instance
.collection("posts")
.get();
Getting a collection will NOT get the subcollections. The subcollections need a separate read.... and reading collection or subcollection considered read.

Firebase cloudstore get all collections in document in flutter [duplicate]

This question already has answers here:
Fetching all collections in Firestore
(3 answers)
Closed 1 year ago.
I am going to get all collections in a document
when I don't know the name of collections, how can I get all collections in a document
await FirebaseFirestore.instance
.collection('answers')
.doc('vS55vOUPtNXaRY06IXIPq6V9ss73')
.collection('uu')
.get()
.then((querySnapshot) {
}).catchError((e){
});
Doument "vS55vOUPtNXaRY06IXIPq6V9ss73" has 2 collections , "uu" and "uy". This is dynamic.
Firestore.instance.collection("answers").snapshots this can be used to retrieve all the documents in firestore

How can I sort Firestore stream based on timestamp in Flutter [duplicate]

This question already has answers here:
how to use orderBy after .where(arrayContain) flutter firebase firestore
(2 answers)
Cloud Firestore: FAILED_PRECONDITION: The query requires an index
(5 answers)
Closed 1 year ago.
I am trying to generate a list using stream builder in flutter and want to sort that list based on firestore timestamp. I have already tried this code given below
```stream: FirebaseFirestore.instance
.collection("posts")
.doc(widget.eid)
.collection("users")
.orderBy("postDate", descending: true)
.where("ownerId", isEqualTo: user.userId)
.snapshots(),```
and the error i got is: "The query requires an index. You can create it here:"
I have resolved this issue by adding indexing on firestore
i think you making two mistake first you useing ordey by before where thats a wrong way to use the where so put where first and then use order by and second your first collection field should not empty if its empty thsts shows italic so you cant fetch data. according firestore its means its delted by defalut. so i make a correction in query.
stream:FirebaseFirestore.instnce.collection("posts") .doc(widget.eid) .collection("users") .where("ownerid",isEqualto:user.userId) .orderBy("postdate",decsending:true) .snapshots(),

How can I use multiple order by in real time database flutter to order data by specific field? [duplicate]

This question already has answers here:
Query based on multiple where clauses in Firebase
(8 answers)
Closed 2 years ago.
I am trying to short "realtime database" result according to "date" in flutter. But I cant use multiple orderByChild('child_name'). It throw an error. My code is
final String path = 'jsondata';
final _dbRef = FirebaseDatabase.instance.reference();
_dbRef.child(path)
.orderByChild('trade_code').equalTo('GP')
.once()
.then((DataSnapshot snapshot) {
snapshot.value.forEach((key, value) {
print(value);
});
});
The result is
Result
Now I want to sort the data by Date.
How can I do that?
It's not possible to use multiple oder at the same time in Firebase Realtime Database. Please check the following doc:
Firebase Realtime Database Query
To achieve that type of complex query I prefer that you should migrate from the Realtime Database to Firebase Cloud Firestore. Check the following resource:
Simple and Complex Dynamic Query in Could Firestore
Your Firestore instance would be:
FirebaseFirestore.instance
.collection('products')
.where('trade_code', isEqualTo: 'GP')
.orderBy('date')

Resources