title: How to sort list based on distance from current user returned by an async function in flutter?
body: <p>I've been trying to sort a list of objects, Here's there code:-</p>
  <pre><code>class Locations {
    final GeoPoint location;
    Locations({this.location});
  }
  
  class LocationCard extends StatefulWidget {
    final int index;
    LocationCard({this.index});
    @override
    _LocationCardState createState() =&gt; _LocationCardState();
  }
  
  class _LocationCardState extends State&lt;LocationCard&gt; {
    @override
    Widget build(BuildContext context) {
      final List&lt;Locations&gt; locationlist = [
        Locations(location: GeoPoint(1, 2)),
        Locations(location: GeoPoint(2, 2)),
        Locations(location: GeoPoint(3, 2)),
        Locations(location: GeoPoint(4, 1))
      ]; //list to be sorted
      GeoPoint mylocation = GeoPoint(2, 2);
      double distance;
      var userindex;
      List sorted = [];
  
      //sort locationlist here
  
      for (var i = 0; i &lt; sorted.length; i++) {
        if (sorted[i].location == mylocation) {
          userindex = sorted.indexOf(locationlist[i]);
        }
      }//gets user's index in list
  
      futureconvert() async {
        distance = await Geolocator().distanceBetween(
                sorted[userindex].location.longitude,
                sorted[userindex].location.latitude,
                sorted[widget.index].location.longitude,
                sorted[widget.index].location.latitude) /
            1000;
        return distance;
      }
  
      return Container(
        child: Card(
          child: Center(
            child: FutureBuilder(
                future: futureconvert(),
                builder: (context, snapshot) {
                  return Text(distance.toString());
                }),
          ),
        ),
      );
    }
  }
  
  class LocationList extends StatefulWidget {
    @override
    _LocationListState createState() =&gt; _LocationListState();
  }
  
  class _LocationListState extends State&lt;LocationList&gt; {
    @override
    Widget build(BuildContext context) {
      final List&lt;Locations&gt; locationlist = [
        Locations(location: GeoPoint(1, 2)),
        Locations(location: GeoPoint(2, 2)),
        Locations(location: GeoPoint(3, 2)),
        Locations(location: GeoPoint(4, 1))
      ]; //list to be sorted
      GeoPoint mylocation = GeoPoint(2, 2);//current user location
  
      List sorted = [];
  
      //sort locationlist here 
  
      return ListView.builder(
          scrollDirection: Axis.horizontal,
          itemCount: sorted.length,
          itemBuilder: (context, index) {
            return LocationCard(index: index);
          });
    }
  }
  </code></pre>
  <p>I need a function that sorts <code>locationlist</code> base on proximity to <code>mylocation</code>.I tried sorting using value returned by this function</p>
  <pre><code>distancef(var x, var y) async {
        return await Geolocator().distanceBetween(
            locationlist[userindex].location.longitude,
            locationlist[userindex].location.latitude,
            x,
            y);
      }
  
  </code></pre>
  <p>But it look's like I have to write my own custom sorting function because I have a future value in return.</p>
  <p>How do I do this?</p>