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

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

Related

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.

Collecting string from Firebase Firestore in flutter

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 to get a subcollection from a document firestore flutter

Im trying to fetch the documents from a subcollection which is in another document, and when I try to fetch the docs and fill a local list with the "docs data" it doesn't fill it, can anyone tell me what I'm doing wrong here?
My method of when I try to fetch the subcollection:
static Stream<List<CheckInOutModel>> employeeCheckInOutStream() {
return firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.snapshots()
.asyncMap((QuerySnapshot querySnapshot) {
final List<CheckInOutModel> employeesCheckInOutList = [];
for (final element in querySnapshot.docs) {
firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.doc(element.id)
.collection('checkInOutList')
.snapshots()
.asyncMap((QuerySnapshot query) {
for (final element in query.docs) {
final employeeCheckInOutModel =
CheckInOutModel.fromDocumentSnapshot(
documentSnapshot: element,
);
employeesCheckInOutList.add(employeeCheckInOutModel);
}
});
}
return employeesCheckInOutList;
});
}
My method when I fetch the fields of the documents that the subcollection is in:
static Stream<List<EmployeeModel>> employeeStream() {
return firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.snapshots()
.map((QuerySnapshot query) {
final List<EmployeeModel> employees = [];
for (final employee in query.docs) {
final employeeModel =
EmployeeModel.fromDocumentSnapshot(documentSnapshot: employee);
employees.add(employeeModel);
}
return employees;
});
}
So I figured out what I did wrong here, I tried to call a stream of it when I only needed it when a callBack is called, so I changed the logic accordingly and went with Future instead Stream
My updated code:
static Future<List<CheckInOutModel>> employeeCheckInOutStream({
required String id,
}) async {
final List<CheckInOutModel> employeesCheckInOutList = [];
final query = await firebaseFirestore
.collection('employees')
.doc(auth.currentUser!.uid)
.collection('employeeList')
.doc(id)
.collection('checkInOutList')
.get();
for (final employee in query.docs) {
final employeeCheckInOutModel = CheckInOutModel.fromDocumentSnapshot(
documentSnapshot: employee,
);
employeesCheckInOutList.add(employeeCheckInOutModel);
}
return employeesCheckInOutList;
}

wait for firestore documents in for loop

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