This is firestore database structure. I'm trying to call these data in my flutter application. but I only get value upto document which is chatroom/9000. But i want collection of document 9000. How do i call it?
This is how i called query
await _firestore .collection('chatroom') .doc(UUID) .get() .then((value) { });
Future<dynamic> getValues() async{
var _chatroom =await _firestore .collection('chatroom').get((value)async{
if(value !=null ){
for(var index = 0 ; index< value.documents.length ; index++ )
var _chatitem = await _firestore .collection('chatroom')
.doc(value.value.documents[index].documentId) .get() .then((value) {
// Do what you Want
})
}
});
}
Related
See the print statement down below. It never executes.
Future<void> populate() async {
final userId = FirebaseAuth.instance.currentUser!.uid;
final db = FirebaseFirestore.instance;
// Get list of ids of parties use swiped on.
var snapshot1 = await db
.collection("partiers_swipes")
.where('userId', isEqualTo: userId)
.get();
var partyIdsUserSwipesOn = [];
if (snapshot1.size > 0) {
snapshot1.docs.forEach((element) {
partyIdsUserSwipesOn.add(element.data()['partyId']);
});
}
var snapshot2 = await db
.collection("parties")
.where(FieldPath.documentId, whereNotIn: partyIdsUserSwipesOn)
.get();
print('This never executes');
}
The whereNotIn argument is not supported by the where clause. This crashes the function.
I am creating like system and i want to get likeCount from firebase which i created.
It's collecting it but returns null,
here is my code:
String? getLikecount(tresc) {
String? likeCount;
FirebaseFirestore.instance
.collection('Posty')
.where('Tresc', isEqualTo: tresc)
.get()
.then((value) => value.docs.forEach((element) async {
var id = element.id;
final value = await FirebaseFirestore.instance.collection('Posty').doc(id).get();
likeCount = value.data()!['likeCount'].toString();
print(likeCount);
}));
print(likeCount);
return likeCount;
}
and here is console output:
Data is loaded from Firestore (and most modern cloud APIs) asynchronously, because it may needs to come from the network and we can't block your code (and your users) while waiting for it.
If we change the print statements a bit, and format the code, it'll be much easier to see what's going on:
String? getLikecount(tresc) {
String? likeCount;
FirebaseFirestore.instance
.collection('Posty')
.where('Tresc', isEqualTo: tresc)
.get()
.then((value) => value.docs.forEach((element) async {
var id = element.id;
final value = await FirebaseFirestore.instance
.collection('Posty')
.doc(id)
.get();
likeCount = value.data()!['likeCount'].toString();
print('In then: $likeCount');
}));
print('After then: $likeCount');
return likeCount;
}
If you run this, you'll see it outputs:
After then: null
In then: 0
This is probably not what you expected, but it explains perfectly why you don't get a result. By the time your return likeCount runs, the likeCount = value.data()!['likeCount'].toString() hasn't executed yet.
The solution is always the same: any code that needs the data from the database has to be inside the then handler, be called from there, or be otherwise synchronized.
In Flutter it is most common to use async and await for this. The key thing to realize is that you can't return something now that hasn't been loaded yet. With async/await you function becomes:
Future<String?> getLikecount(tresc) {
String? likeCount;
var value = await FirebaseFirestore.instance
.collection('Posty')
.where('Tresc', isEqualTo: tresc)
.get();
for (var doc in value.docs) {
var id = element.id;
final value = await FirebaseFirestore.instance
.collection('Posty')
.doc(id)
.get();
likeCount = value.data()!['likeCount'].toString();
print('In then: $likeCount');
}));
print('After then: $likeCount');
return likeCount;
}
Now your code returns a Future<String?> so a value that at some point will hold the string. When calling getLikecount you will now need to use then or await to handle the Future, and if you want to show the count in the UI you will have to store it in the State of a StatefulWidget.
How do I loop through a DocumentSnapshot.data() object and retrieve its keys and values?
My code currently looks like this:
Future<void> drawPolygons() async {
var points = await FirebaseFirestore.instance
.collection('polygons')
.doc(uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
//if user has polygon data on account
var data = documentSnapshot.data();
}
});
}
**Update Your Method and Convert the data to Map**
Future<void> drawPolygons() async {
var points = await FirebaseFirestore.instance
.collection('polygons')
.doc(uid)
.get()
.then((DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
Map data = (documentSnapshot.data() as Map);
for (var entry in data.values) {
print(entry);
}
}
});
}
I made this function to get document(post)'s userId but docu always becomes null.
Is there any ideas to solve this problem?
(I use flutter and cloud firestore)
Future<String> getUserId(String text) async{
var docu;
await FirebaseFirestore.instance
.collection('post')
.where('content', isEqualTo: '$text')
.snapshots()
.listen((snapshot){
snapshot.docs.forEach((document) {
docu = document;
print('docUserId : ${document.data()['userId']}'); // this works well
});
});
return docu.data()['userId']; }
Also I tried return only document userId with String like this
Future<String> getUserId(String text) async{
String docUserId;
await FirebaseFirestore.instance
.collection('post')
.where('content', isEqualTo: '$text')
.snapshots()
.listen((snapshot){
snapshot.docs.forEach((document) {
docUserId = document.data()['userId'];
print('docUserId : ${docUserId}'); // this works well
});
});
return docUserId; }
Although print('docUserId : ${docUserId}'); this command works well, the final return value is always null.
I can't find the reason.
You need to take care of 3 importand points here:
You don't need to have a realtime listener if you just want to get the data once
You can't await a listener. Your function will always end before you get the data.
Check if a document exists before you use it
Could you pls try it with this code:
import 'package:cloud_firestore/cloud_firestore.dart';
Future<String> getUserId(String text) async {
String userUid = '';
QuerySnapshot docSnap = await FirebaseFirestore.instance
.collection('post')
.where('content', isEqualTo: '$text')
.get();
docSnap.docs.forEach((DocumentSnapshot document) {
if (document.exists) {
userUid = document.data()['userId'];
print('docUserId : ${document.data()['userId']}');
}
});
return userUid;
}
document.data()
change this to
docu = document;
print('docUserId : ${docu['userId']}');
trying to fetch results within a for loop , but for loop doesn't wait for firestore results.tried forEach as well before .
Future<bool> checkIfNewMessages() async{
bool hasNewMessage=false;
QuerySnapshot _myDoc = await Firestore.instance.collection('PropMap')
.orderBy('ProJoiningDate')
.where('TenId', isEqualTo: globals.memberAuthId)
.getDocuments();
List<DocumentSnapshot> properties = _myDoc.documents;
if(properties.length>0)
for(final property in properties) {
//properties.forEach((property) { //tried forEach() as well
String propid= property.data['PropertyId'];
if(property.data['LastVisitTime']!=null) {
DateTime tenantsLastPropVisitTime = property.data['LastVisitTime'].toDate();
getLastPropertyChatTime(propid).then((latestPropChatTime) { //This 'then' seems not working
print('LAST chat date is ${latestPropChatTime}');
if (latestPropChatTime.isAfter(tenantsLastPropVisitTime)) //This means he has not seen new messages , lets notify him
{
hasNewMessage= true;
}
});
}
};
return hasNewMessage;
}
And these are the fetch methods,when the breakpoint is at getDocuments() of getTheLastChat() the control just jumps back to for loop again without waiting for results .
Future getTheLastChat(propId) async {
QuerySnapshot _myDoc =await Firestore.instance.collection('Chats').orderBy('ChatDate', descending: true)
.where('PropertyId', isEqualTo: propId)
.limit(1)
.getDocuments();
List<DocumentSnapshot> tenants = _myDoc.documents;
return tenants;
}
Future<DateTime> getLastPropertyChatTime(propId) async {
DateTime lastChatTime= DateTime.now().add(Duration(days:-365));
var lastChatTimeDocs = await getTheLastChat(propId);
lastChatTime=lastChatTimeDocs.length>0?lastChatTimeDocs[0].data["ChatDate"].toDate():DateTime.now().add(Duration(days:-365));
return lastChatTime;
}
You can use the Future.forEach to achieve your requirement
Future<void> buildData(AsyncSnapshot snapshot) async {
await Future.forEach(snapshot.data.documents, (element) {
employees.add(Employee.fromSnapshot(element));
});
}