dart - Flutter 在 ListView.builder 中显示未来值
问题描述
如何在 listview.builder(loop) 的 Text 小部件中显示未来值?我正在计算每个属性的距离,并希望在文本小部件中显示。
Text('${ _calculateDistance(model, store)' }
它返回一个“未来的实例”
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;}
对于单个帖子,我知道我可以使用 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(),
);
}
},
);
}
推荐阅读
- javascript - Adobe javascript:需要获取字段的值(SCORE)并返回文本(PASS/FAIL)
- mongodb - Heroku mLab MongoDB 插件与 MongoDB Atlas
- powershell - foreach中字符串连接的空白结果
- amazon-web-services - 在 EC2 实例中使用 Selenium 的问题
- json - python 3:从sqlite反序列化嵌套字典
- webpack - 在 Webpack 和 Angular 构建期间提高内存使用率
- python - Django (fields.E304) - AbstractUser
- router - 如何在 Windows 环境下知道路由器的 ExternalIPAddress
- java - Android - 从通知中打开活动时,getCurrentUser() 返回 null
- html - 当我将链接添加到我的个人资料时,LinkedIn og:image 标签不起作用,但在我发帖时起作用