如何在 listview.builder(loop) 中的 Text 小部件中显示未来值?我正在计算每个属性的距离并希望显示在文本小部件中。
Text('${ _calculateDistance(model, store)' }
Run Code Online (Sandbox Code Playgroud)
返回“未来实例”
Future<double> _calculateDistance(MainModel model, Store store) async {
double distanceInMeters = await Geolocator().distanceBetween(
model.postion.latitude,
model.postion.longitude,
store.position.latitude,
store.position.longitude);
return distanceInMeters / 1000;}
Run Code Online (Sandbox Code Playgroud)
对于单个帖子,我知道我可以使用 setstate,但我正在计算每个帖子的距离。
您可以将您的物品包裹在一个 FutureBuilder
@override
Widget build(BuildContext context) {
return FutureBuilder<double>(
future: _calculateDistance( model, store),
initialData: 0.0,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text("${snapshot.data}");
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3204 次 |
| 最近记录: |