This how i declare my list
List<String> distance = [];
and here is my ListView builder
return ListView.builder(
itemCount: widget.stores.length,
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemBuilder: (BuildContext context, int index) {
if (widget.latitude == 0.0) {
} else {
calculateDistance(widget.stores[index].storeLatitude,
widget.stores[index].storeLongitude, index);
}
return GestureDetector(
child: Card(
child: Container(
width: MediaQuery.of(context).size.width * 0.50,
child: ListTile(
title: Text(
widget.stores[index].storeName,
style: TextStyle(fontSize: 18),
),
subtitle: Text(
widget.stores[index].storeAddress,
),
trailing: (distance[index].isEmpty) ? Text("") : Text(distance[index]),
),
),
),
);
});
i'am adding distance inside the setState()
here is the function to add value for distance
calculateDistance(widget.stores[index].storeLatitude,
widget.stores[index].storeLongitude, index);
-------------
Future calculateDistance(String storeLatitude, String storeLongitude) async {
final list = (await Geolocator().distanceBetween(widget.latitude, widget.longitude,double.parse(storeLatitude), double.parse(storeLongitude)));
if (list != null) {
setState(() {
distance.add((list / 1000).toStringAsFixed(1));
});
}
}
so when it is empty, it is throwing this error
I/flutter (22854): Another exception was thrown: RangeError (index): Invalid value: Valid value range is empty: 0 I/flutter (22854): Another exception was thrown: RangeError (index): Invalid value: Valid value range is empty: 1
how can i fix it ?
here is my full script https://gist.github.com/bobykurniawan11/ef711e5121be303e8102a6ab4871435f