2

So basically I want to add results of two queries into one query(removing duplicates) and than display. How can I do it?

Future<QuerySnapshot> allUsersProfile = usersReference.where("searchName", isGreaterThanOrEqualTo: str.toLowerCase().replaceAll(' ', '')).limit(5).get();
Future<QuerySnapshot> allSameUsersProfile = usersReference.where("searchName", isEqualTo: str.toLowerCase().replaceAll(' ', '')).limit(5).get();

Future<QuerySnapshot> futureSearchResults;

displayUsersFoundScreen(){
return FutureBuilder(
  future: futureSearchResults,
  builder: (context, dataSnapshot){

I want to store results of both into futureSearchResults only, as I use Future Builder to display the results.

Can any body can help me? Thanks in advance.

Harsh Patel
  • 199
  • 1
  • 2
  • 15
  • Does this answer your question? [How to merge two firestore queries in the Flutter project and How do I remove the duplicate documents from these two streams?](https://stackoverflow.com/questions/61053852/how-to-merge-two-firestore-queries-in-the-flutter-project-and-how-do-i-remove-th) – MickaelHrndz Mar 23 '21 at 09:28

1 Answers1

1

It is possible to use the Future.wait method in this case. The builder callback will await for all futures to resolve and the result in a List.

FutureBuilder(
    future: Future.wait([
         allUsersProfile(), 
         allSameUsersProfile(),
    ]),
    builder: (
       context,
       AsyncSnapshot<List<QuerySnapshot>> snapshot, 
    ){
         // ...
    }
);


Other option if you want to merge documents property is to implement a futureSearchResults function that will call futures and apply transformation.

Future<QuerySnapshot> futureSearchResults() async {
  final future1 = await allUsersProfile();
  final future2 = await allSameUsersProfile();
  
  future1.data.documents.add(future2.data.documents);
  
  return future1;
}

glavigno
  • 483
  • 5
  • 10
  • 1
    in this I can't use dataSnapshot.data.documents.forEach((document). It is giving error that document isn't defined for the type 'List' – Harsh Patel Mar 23 '21 at 09:41
  • please check my edited answer, i added an option to merge documents property. – glavigno Mar 23 '21 at 09:59