Flutter:如何提取未来的对象,以便与普通对象一样使用它?
我想从HTTP请求中提取未来对象,以便可以将其用作小部件中的常规对象。我该如何实现? (下面的抽象代码示例)
class _SomePageState extends State<SomePage> {
Data? _data;
@override
void initState() {
_loadData();
super.initState();
}
void _loadData() async {
await Service.getData().then((value) {
setState(() {
_data = value;
});
});
}
@override
Widget build(BuildContext context) {
//further use of the _data variable
}
}
在这种情况下,我将有延迟的错误错误。总的来说,我想听听有关使用解释的未来对象的简短建议。谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,当在
构建方法中使用
的工作时,我们使用在这里是Google团队的视频,介绍了如何使用它。
在您的示例中:
在
_loaddata()
函数中:在
构建
方法中Usually when working with
Future
's within thebuild
method, we useFutureBuilder
.Here is a video from the Google team on how to use it.
In your example:
In the
_loadData()
function:In the
build
method