【发布时间】:2019-02-24 14:07:20
【问题描述】:
我目前有一个应用程序可以显示 1.5 公里半径内的附近医院,它看起来像这样:
我遇到的麻烦是,我无法根据计算出的从最低到最高的距离对卡片进行排序。
我创建了一个List<double> doubleList = []; 来存储计算出的距离列表,并使用doubleList.sort(); 对其进行排序。
这里是sn-p的代码:
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();
}
});
这是打印时排序距离值的输出:
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]
我的问题是:
我如何确保小部件将遵循排序距离值的顺序?
【问题讨论】: