Reputation: 5768
I have a streambuilder, within, I want to calculate the distance for each different card of the streambuilder. It calculates the distance if I print it in the terminal, but on the screen it shows: Instance of future < String >. Is there a fix for this? I tried making a variable and run set state, but the value has to be different for each card of the streambuilder.
Future<String> distanceInMeters({double uLat, double uLong}) async {
if (uLat == null && uLong == null) {
return "";
} else {
double distanceInMeters =
await Geolocator().distanceBetween(uLat, uLong, 52.3546274, 4.8285838);
String distance = distanceInMeters.toString();
return "$distance";
}
}
Upvotes: 2
Views: 1556
Reputation: 126604
You will need to use a FutureBuilder
. If you are not familar with this widget, you can follow the link to the documentation, which describes the function of the widget extensively.
In practice, it would look something like this:
FutureBuilder(
future: distanceInmeters(uLat: uLat, uLong: uLong),
builder: (context, AsyncSnapshot<String> snapshot) {
if (snapshot.hasError) return Text('${snapshot.error}');
if (snapshot.hasData) return Text('${snapshot.data}');
return const CircularProgressIndicator();
},
)
The reason you need this is that you are returning your String
value from an asynchronous function and thus you need to have some logic that waits to extract the result from the Future
.
Upvotes: 1