How to send custom data to future builder?

105

You can accumulate your results into a list and return that list.

Future makeRequest() async {
   var results =[];
   var firestore = Firestore.instance;
   QuerySnapshot qn = await firestore
   .///
       .collection('mechanic')
      .getDocuments();

   final Distance distance = new Distance();

   qn.documents.forEach((f) {
       double km = distance.as(
       LengthUnit.Kilometer,
       new LatLng(f.data['location'].latitude, f.data['location'].longitude),
       new LatLng(widget.lat, widget.lan));
       if (km < f.data['radius']) { 
           results.add(f.data);
       }

   });

   return results;
}
Share:
105
Ilthizam Imtiyas
Author by

Ilthizam Imtiyas

Updated on December 16, 2022

Comments

  • Ilthizam Imtiyas
    Ilthizam Imtiyas over 1 year

    I am trying to find the nearest people from my current location. So I am fetching all the documents from firestore.

    My firestore structure is like this,

    So I am fetching all the users and checking the nearest users by calculating the distance and I want to pass the filtered data to the future builder to view.

    Future makeRequest() async {
        var firestore = Firestore.instance;
        QuerySnapshot qn = await firestore
             .///
            .collection('mechanic')
            .getDocuments();
    
        final Distance distance = new Distance();
    
        qn.documents.forEach((f) {
          double km = distance.as(
              LengthUnit.Kilometer,
              new LatLng(f.data['location'].latitude, f.data['location'].longitude),
              new LatLng(widget.lat, widget.lan));
          if (km < f.data['radius']) {
            print(f.data);
            // filtering data and want to pass this filterd data to Future builder
          }
        });
    
        return qn.documents;
      }
    
    body: FutureBuilder(
                future: makeRequest(),
                builder: (_, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return Center(
                      child: Text("Loading..."),
                    );
                  } else {
                    return ListView.builder(
                      itemCount: snapshot.data.length,
                      itemExtent: 100.0,
                      itemBuilder: (_, index) {
                        return ListTile(
                          title: Text(
                            snapshot.data[index].data['name'],
                        );
                      },
                    );
                  }
                })
    

    How can solve this?

    • easeccy
      easeccy over 4 years
      What is the issue here? Are you getting any errors?
    • Ilthizam Imtiyas
      Ilthizam Imtiyas over 4 years
      @easeccy No. From the if statement I am trying to filter the fetched data and I just want to pass it to Future builder. But in the above code , return of ' qn.documents;' just returning the all fetched value to future builder. But I only want to return filterd data