Reputation: 3071
I currently have an app that displays nearby hospitals within a 1.5 km radius and it looks like this:
The trouble I am having is that I could not figure out how to sort the card based on their calculated distance from lowest to highest.
I made a List<double> doubleList = [];
to store the list of calculated distances and sorted it with doubleList.sort();
.
Here is the code snippet:
return new ListView.builder(
shrinkWrap: true,
itemCount: jsonresponse.length,
itemBuilder: (context, index){
String name = jsonresponse[index]["Name"];
String lat = jsonresponse[index]["Latitude"];
String lng = jsonresponse[index]["Longitude"];
var distance = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(lat), longitude2: double.parse(lng));
var totaldistance = distance.haversineDistance().toStringAsFixed(2);
double distanceDouble = double.parse(totaldistance);
if(distanceDouble < 1500.00){
doubleList.add(distanceDouble);
doubleList.sort((a, b){
return a.compareTo(b);
});
print(doubleList);
return new Column(
children: <Widget>[
new Card(
child: new ListTile(
title: new Text(name),
subtitle: new Text("$distanceDouble m away"),
trailing: new IconButton(
icon: new Icon(Icons.info),
onPressed: (){
makeDialog(lat, lng);
}),
),
)
],
);
}else{
return new Container();
}
});
This is the output of the sorted distance values when printed:
I/flutter ( 4286): [987.8]
I/flutter ( 4286): [987.8, 1014.87]
I/flutter ( 4286): [987.8, 1014.87, 1352.22]
I/flutter ( 4286): [128.26, 987.8, 1014.87, 1352.22]
I/flutter ( 4286): [128.26, 987.8, 1014.87, 1260.73, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1260.73, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1260.73, 1303.36, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1232.47, 1260.73, 1303.36, 1352.22]
I/flutter ( 4286): [122.91, 128.26, 987.8, 1014.87, 1232.47, 1232.47, 1260.73, 1303.36, 1352.22]
My question is:
How do I make sure the widget will follow the order of the sorted distance values?
Upvotes: 2
Views: 10680
Reputation: 29438
You can try something like this
jsonresponse.sort((a, b) {
var distance1 = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(a.lat), longitude2: double.parse(a.lng));
var totaldistance1 = distance1.haversineDistance().toStringAsFixed(2);
double distanceDouble1 = double.parse(totaldistance1);
var distance2 = new GreatCircleDistance.fromDegrees(latitude1: double.parse(widget.lat), longitude1: double.parse(widget.lng), latitude2: double.parse(b.lat), longitude2: double.parse(b.lng));
var totaldistance2 = distance2.haversineDistance().toStringAsFixed(2);
double distanceDouble2 = double.parse(totaldistance2);
return (distanceDouble1 - distanceDouble2).toInt();
});
before return new Column (I set toInt because double is not suitable for comparator)
Upvotes: 2
Reputation: 5894
ListView.builder
is taking an itemBuilder wich is responsible of building one item. careful here, your sorting each time you build an item.
You need to sort your jsonresponse
before calling List.builder
or jsonresponse[index]
won't be correct.
Upvotes: 6