wait for firestore documents in for loop - flutter

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));
});
}

Related

How to fetch a `DocumentReference` from a Firebase `get()`

I have a collection ads that contains a DocumentReference as ownerId.
With the code below, I am able to fetch the 10 most recent ads as aList<Ad>:
/// Returns a list of ads of the given [category]
static Future<List<ClassifiedAd>> getFromCategory(
ClassifiedAdCategory category,
{int max = 10}) async {
return FirebaseFirestore.instance
.collection('ads')
.where('category', isEqualTo: category.name)
.orderBy('creationDate', descending: true)
.limit(max)
.get()
.then((snapshot) {
return snapshot.docs.map((doc) {
final data = doc.data();
return Ad.fromMap(data);
}).toList();
});
But now I'd like to fetch the owner (collection users) from the DocumentReference I was talking about above. But I am a but puzzled about how to do that.
My modified code below does not compile:
The return type 'List' isn't a 'FutureOr<List>', as required by the closure's context.
/// Returns a list of ads of the given [category]
static Future<List<ClassifiedAd>> getFromCategory(
ClassifiedAdCategory category,
{int max = 10}) async {
return FirebaseFirestore.instance
.collection('ads')
.where('category', isEqualTo: category.name)
.orderBy('creationDate', descending: true)
.limit(max)
.get()
.then((snapshot) {
// <<<< Error starts right here down to the removeWhere()
return snapshot.docs.map((doc) {
final data = doc.data();
final DocumentReference docRef = data["ownerId"];
return docRef.get().<ClassifiedAd?>then((snapshot) {
if (snapshot.exists) {
return ClassifiedAd.fromMap(data);
}
return null;
});
}).toList()
// Don't take into account potential nulls
..removeWhere((a) => a == null);
});
How should I do that?
I would say that the wrong thing that you're doing is you're trying to get a snapshot asynchronously inside the map() method which is synchronous, for such cases like yours, I recommend using await/async and to not return anything until you guarantee that you got it, try this:
static Future<List<ClassifiedAd>> getFromCategory(
ClassifiedAdCategory category,
{int max = 10}) async {
final snapshot = await FirebaseFirestore.instance
.collection('ads')
.where('category', isEqualTo: category.name)
.orderBy('creationDate', descending: true)
.limit(max)
.get();
List<ClassifiedAd> result = [];
for (int index = 0; index < snapshot.docs.length; index++) {
final doc = snapshot.docs[index];
final data = doc.data();
final DocumentReference docRef = data["ownerId"];
final docOwnerSnapshot = await docRef.get();
if (docOwnerSnapshot.exists) {
result.add(ClassifiedAd.fromMap(data));
}
}
return result;
}

Results are different when application tested on a physical phone and on simulator

I am trying to solve an issue with my application. When I test it on a virtual device (iPhone), the query is working well and I am getting the document I am supposed to get. When I test the application on my physical phone, the application does not find any record.
I have checked the filters, they are the same. it is exactly the same code. I have never have this situation before. Please, do you have any suggestion?
Future myQuery ( time, energy, urgent, important ) async{
final uid = FirebaseAuth.instance.currentUser!.uid;
final path = 'Users/$uid/allTasks';
final currentQuery = FirebaseFirestore.instance.collection(path);
Query statusQuery = currentQuery.where('status', isEqualTo: 'Inbox');
// Query contextQuery = statusQuery.where('context', isEqualTo: );
Query timeQuery = statusQuery.where('time_Needed', isEqualTo: time);
Query energyQuery = timeQuery.where('energy_Needed', isEqualTo: energy);
Query urgentQuery = energyQuery.where('urgent', isEqualTo: urgent);
Query importantQuery = urgentQuery.where('important', isEqualTo: important);
final snapshot = await importantQuery.get();
final data = snapshot.docs;
if(data.isNotEmpty) {
return snapshot;
}
}
ElevatedButton(child:
const Text('FIND'),
onPressed: () async {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
EngageDisplayTasks(
time: timeSelectedPicker!, energy: energySelectedPicker!,
urgent: urgentWhere, important: importantWhere,
)));
}
),
#override
void initState() {
super.initState();
myQuery(time,energy,urgent,important).then((results) {
setState(() {
querySnapshot = results;
});
});
queryEngage(time,energy,urgent,important).then((results) {
setState(() {
querySnapshot = results;
});
});
}
Future queryEngage (time,energy,urgent,important) async {
await myQueryV2();
await myQueryV3 (statusQuery,time);
await myQueryV4 (timeQuery,energy);
await myQueryV5 (energyQuery,urgent);
await myQueryV6 (urgentQuery,important);
}
Future myQueryV2 ( ) async{
final uid = FirebaseAuth.instance.currentUser!.uid;
final path = 'Users/$uid/allTasks';
final currentQuery = FirebaseFirestore.instance.collection(path);
statusQuery = currentQuery.where('status', isEqualTo: 'Inbox');
return statusQuery;
}
Future myQueryV3 (statusQuery, time) async {
timeQuery = statusQuery.where('time_Needed', isEqualTo: time);
return timeQuery;
}
Future myQueryV4 (timeQuery, energy) async {
energyQuery = timeQuery.where('energy_Needed', isEqualTo: energy);
return energyQuery;
}
Future myQueryV5 (energyQuery, urgent) async {
urgentQuery = energyQuery.where('urgent', isEqualTo: urgent);
return urgentQuery;
}
Future myQueryV6 (urgentQuery, important) async {
importantQuery = urgentQuery.where('important', isEqualTo: important);
print ('urgent: $urgent');
print ('important: $important');
print ('time: $time');
print ('energy: $energy');
final snapshot = await importantQuery.get();
final data = snapshot.docs;
if(data.isNotEmpty) {
return snapshot;
}
}
You could write a single compound query like this. It is not necessary to get the results at each stage based on the code shown. You may need to create some composite indexes to improve optimization, but Firebase should notify you automatically if this is the case.
Future myQuery ( time, energy, urgent, important ) async{
final uid = FirebaseAuth.instance.currentUser!.uid;
final path = 'Users/$uid/allTasks';
final currentQuery = FirebaseFirestore.instance.collection(path);
try {
final snapshot = currentQuery
.where('status', isEqualTo: 'Inbox')
.where('time_Needed', isEqualTo: time)
.where('energy_Needed', isEqualTo: energy)
.where('urgent', isEqualTo: urgent)
.where('important', isEqualTo: important);
await snapshot.get().then((data) {
for (var doc in data.docs) {
print("${doc.id} => ${doc.data()}");
}
});
} catch (err) {
print(err);
}
}

async function not completing when querying FirebaseFirestore

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.

Flutter, Couldn't get firebase query snapshots

I fetching data from firebase performing a query and updating a bool based on the query. But my code doesn't work. I tried printing the snapshots but I get "Instance of '_ControllerStream<QuerySnapshot<Map<String, dynamic>>>'" in the terminal how do I solve this.
Stream<QuerySnapshot<Map<String, dynamic>>>namees = await FirebaseFirestore
.instance
.collection('lender')
.doc(auth.currentUser!.email)
.collection('borrowers')
.where('Name', isEqualTo: textfieldname).snapshots();
print(namees);
if (namees.isEmpty == true) {
setState(() {
isThere = true;
});
} else {
setState(() {
isThere = false;
});
}

I need some guidance in the Future Asynchronous Calls with flutter and dart, sometimes things happen out of order

The following code works fine, because it return only a simple list, but in some cases that I need to do nested Firebase calls, I can't make things happen in the right order, and the main return statement comes incomplete. What can I do to improve my Future Asynchronous Calls?
Future<List<MyNotification>> getNotifications() async {
var uid = await FirebaseAuth.instance.currentUser();
List<MyNotification> tempNots = await Firestore.instance
.collection("notifications")
.where("targetUsers", arrayContains: uid.uid)
.getDocuments()
.then((x) {
List<MyNotification> tempTempNots = [];
if (x.documents.isNotEmpty) {
for (var not in x.documents) {
tempTempNots.add(MyNotification.fromMap(not));
}
}
return tempTempNots = [];
});
return tempNots;
}
The most important thing; don't use then inside your async functions. I modified your code like this;
Future<List<MyNotification>> getNotifications() async {
// Using the type definition is better.
FirebaseUser user = await FirebaseAuth.instance.currentUser();
// The return type of getDocuments is a QuerySnapshot
QuerySnapshot querySnapshot = await Firestore.instance
.collection("notifications")
.where("targetUsers", arrayContains: user.uid)
.getDocuments();
List<MyNotification> tempTempNots = [];
if (querySnapshot.documents.isNotEmpty) {
for (DocumentSnapshot not in querySnapshot.documents) {
tempTempNots.add(MyNotification.fromMap(not));
}
}
return tempTempNots;
}