Ilthizam Imtiyas
Ilthizam Imtiyas

Reputation: 179

How to send custom data to future builder?

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?

Upvotes: 1

Views: 127

Answers (1)

easeccy
easeccy

Reputation: 5086

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

Upvotes: 2

Related Questions