I want to get all the document IDs in my firebase and store it in List but I’m only getting one of the documents. Here is my execution
my code
Future saveUserCart(User currentUser) async
{
List<String> IDs = [];
Future getDocs() async {
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('users')
.doc(currentUser.uid)
.collection("userCart").get();
for (int i = 0; i < querySnapshot.docs.length; i++) {
var itemIDs = querySnapshot.docs[i];
print(itemIDs.id);
IDs = [itemIDs.id];
}
print(IDs);
}
getDocs();
}
Fix my problem and learn something
3
Answers
Try IDs.add(itemIDs.id); instead of IDs=[itemIDs.id];
Instead of adding the code in question is creating a new list and assigning it to the last id. When we use add method we can get all ids from the documents.
It’s just Example,
you can use base on your requirements. For gettings "ID" you don’t need to use for loop, use "map" functions.
Expected Output,
Maybe, it will help you.