颤振-为每个Listview项运行异步函数

颤振-为每个Listview项运行异步函数,listview,geolocation,flutter,Listview,Geolocation,Flutter,我有一个在Listview生成器中检索和显示的位置列表 每个位置都有一个纬度和经度值 我现在尝试使用GeoLocator插件()将“距离”字段包含到Listview中,方法是使用“查找最近的”按钮获取用户位置,然后使用用户的lat/lon和每个位置的lat/lon计算距离 由于“GeoLocator计算距离方法”是异步的,因此无法插入GeoLocator().DistanceBeween(userLat, userLon、storeLat、storeLon)直接导入小部件 问题: 那么,当用户单

我有一个在Listview生成器中检索和显示的位置列表

每个位置都有一个纬度和经度值

我现在尝试使用GeoLocator插件()将“距离”字段包含到Listview中,方法是使用“查找最近的”按钮获取用户位置,然后使用用户的lat/lon和每个位置的lat/lon计算距离

由于“GeoLocator计算距离方法”是异步的,因此无法插入GeoLocator().DistanceBeween(userLat, userLon、storeLat、storeLon)直接导入小部件

问题: 那么,当用户单击“查找最近的”按钮时,每个Listview项都会通过getDistance方法传递并返回一个结果,这是如何做到的呢

简化代码:

Future<List> getdata() async {
final response = await http.get(getStoreLocations);
return json.decode(response.body);
}


@override
Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(title: new Text("Location"),),
  body: new FutureBuilder<List>(
    future: getdata(),
    builder: (context, snapshot) {
      if (snapshot.hasError) print(snapshot.error);
      return snapshot.hasData
          ? new BrowseStoreLocation(
          list: snapshot.data,)
          : new Center(
        child: new CircularProgressIndicator(),
      );
    },
  ),
);
}
}
地理定位器计算距离方法:

getDistance()async {
double distanceInMeters = await Geolocator().distanceBetween(userLat, 
userLon, storeLat, storeLon);
distance = distanceInMeters/1000;
}
我试着通过设置变量进行实验。打印显示所有项目的storeLat和storeLon值,但只计算并返回最后一个listitem

String storeLat = "";
String storeLon = "";
getDistance(storeLat,storeLon)async {
double distanceInMeters = await Geolocator().distanceBetween(userLat, userLon, double.parse(storeLat), double.parse(storeLon));
distance = distanceInMeters/1000;
setState(() {
});
}

new ListView.builder(
itemCount: widget.list == null ? 0 : widget.list.length,
itemBuilder: (context, i) {
storeLat = widget.list[i]['latitude'];
storeLon = widget.list[i]['longitude'];
print(storeLon+storeLat);
return Container(
child: Column(
children: <Widget>[
new Text(distance.toString())
],
),
String storeLat=”“;
字符串storeLon=“”;
getDistance(storeLat、storeLon)异步{
double distanceInMeters=await Geolocator().distanceBetween(userLat,userLon,double.parse(storeLat),double.parse(storeLon));
距离=距离(米/1000);
设置状态(){
});
}
新建ListView.builder(
itemCount:widget.list==null?0:widget.list.length,
itemBuilder:(上下文,i){
storeLat=widget.list[i]['latitude'];
storeLon=widget.list[i]['longitude'];
打印(storeLon+storeLat);
返回容器(
子:列(
儿童:[
新文本(distance.toString())
],
),
您必须在内部使用此方法,然后必须在内部调用您的方法
getDistance

ListView.builder(
  itemCount: locationsList.length,
  itemBuilder: (context, position) {
    final current = locationsList[position];
    return FutureBuilder<String>(
      future: getDistance(current.latitude, current.longitude)
                   .then((location) => location.toString()),
      builder: (context, snapshot) {
        return Container(
          child: Column(
            children: <Widget>[new Text(snapshot.data)],
          ),
        );
     });
});
ListView.builder(
itemCount:位置列表长度,
itemBuilder:(上下文、位置){
最终电流=位置列表[位置];
回归未来建设者(
future:getDistance(当前.纬度,当前.经度)
.然后((位置)=>location.toString()),
生成器:(上下文,快照){
返回容器(
子:列(
子项:[新文本(快照.数据)],
),
);
});
});

Hi Damien,你找到解决方案了吗?我几天后也遇到了同样的问题……我不知道如何让ListView项等待查询。对于我来说,我刚刚让ListView生成器返回一个有状态类,然后在这里有一个Futurebuilder为每个ListView项运行异步函数。你能发布这个答案吗?我非常感谢你关于此问题的第二个解决方案,请将其发布在此处。谢谢。通过此链接,我们能够解决此问题:
ListView.builder(
  itemCount: locationsList.length,
  itemBuilder: (context, position) {
    final current = locationsList[position];
    return FutureBuilder<String>(
      future: getDistance(current.latitude, current.longitude)
                   .then((location) => location.toString()),
      builder: (context, snapshot) {
        return Container(
          child: Column(
            children: <Widget>[new Text(snapshot.data)],
          ),
        );
     });
});