我在您的代码中看到了多个错误。
首先,你的代码无法编译
-
Double
Dart 语言中 type 为小写。
- 你不应该初始化
double
带有空的变量String
.
其次,您将全局状态与异步调用结合使用。如果您只是将参数传递给 getDistance 方法,那就更好了。像这样:
Future<double> getDistance(double lat, double long) async {
final distanceInMeters = await Geolocator().distanceBetween(
currentLocation.latitude,
currentLocation.longitude,
lat,
lng
);
return distanceInMeters / 1000;
}
最后你应该使用未来建造者 https://api.flutter.dev/flutter/widgets/FutureBuilder-class.html打电话getDistance
:
ListView.separated(
itemCount: content.length,
separatorBuilder: (context, position) {
// return a separator widget;
},
itemBuilder: (context, position) {
final current = content[position];
return FutureBuilder<String>(
future: getDistance(current.lat, current.long)
.then((value) => value.toString()),
builder: (context, snapshot) {
return Container(
child: Column(
children: <Widget>[new Text(snapshot.data)],
),
);
});
});