Я пытаюсь показать расстояние между несколькими пользователями, которые зарегистрированы в приложении и отображаются в Listview
, а код для Listview
приведен ниже: -
class DistanceListView extends StatefulWidget {
@override
_DistanceListViewState createState() => _DistanceListViewState();
}
class _DistanceListViewState extends State<DistanceListView> {
@override
Widget build(BuildContext context) {
final profiles = Provider.of<List<ProfileData>>(context) ?? [];
return ListView.builder(
itemCount: profiles.length,
itemBuilder: (context, index) {
return DistanceCard(profile: profiles[index], index: index);
});
}
}
Где profiles
- это список, содержащий все местоположения пользователей в нем. Прямо сейчас я создал двух фиктивных пользователей с координатами 35.6762, 139.6503 и 6.9271, 79.8612 .
Код для DistanceCard
выглядит следующим образом: -
class DistanceCard extends StatefulWidget {
final int index;
final ProfileData profile;
DistanceCard({this.index, this.profile});
@override
_DistanceCardState createState() => _DistanceCardState();
}
class _DistanceCardState extends State<DistanceCard> {
@override
Widget build(BuildContext context) {
final profiles = Provider.of<List<ProfileData>>(context) ?? [];
final longitude = widget.profile.location.longitude;
final latitude = widget.profile.location.latitude;
var distance;
futureconvert() async {
distance = await Geolocator().distanceBetween(
longitude,
latitude,
profiles[widget.index].location.longitude,
profiles[widget.index].location.latitude);
return distance;
}
return FutureBuilder(
future: futureconvert(),
builder: (context, snapshot) {
return Card(
child: Center(
child: Padding(padding: EdgeInsets.all(5),
child: Text(distance)),
),
);
});
}
}
Вот проблема, когда виджет отображается, distance
всегда отображается как 0,0 . Кто-нибудь может указать, где я ошибся?