gpt4 book ai didi

dart - future build 者( future :) is recursing my http requests,

转载 作者:IT王子 更新时间:2023-10-29 06:53:54 25 4
gpt4 key购买 nike

我需要在页面呈现之前从三个不同的 url 获取数据。所以,这是我的 ScopedModel 中的方法,包括多个 http.post 方法:

Future fetchData() async {
_isLoading = true;
notifyListeners();
await fetchAvailable();
await fetchOnProgress();
await fetchCompleted();
_isLoading = false;
notifyListeners();

fetchData 区域中的方法只是带有原始 Future 类型的经典 http.post 请求。

这是我的 FutureBuilder:

FutureBuilder(
future: model.fetchData(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
return snapshot.connectionState != ConnectionState.done
? Center(
child: CircularProgressIndicator(),
)
: Column.......

问题是,'future' 函数,它不断执行并且永不结束。我的算法是从服务器获取 json,将 body 中的变量膨胀到 ListView.builder 后代中。

输出如我所说的递归发布请求。此外,我收到此日志,行数递增,如 1 - 2 - 3 或 2 - 4 - 6 -8 等。

uid=10085(my.package.directory) 1.ui identical 1 lines
.......Other logs here
uid=10085(my.package.directory) 1.ui identical 3 lines

然后就这样继续下去......

此外,还有其他有用的方法可以在页面呈现之前处理少量数据吗?

最佳答案

不要在 FutureBuilder 中发出 HTTP 请求

https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html

The future must have been obtained earlier, e.g. during State.initState, State.didUpdateConfig, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.

关于dart - future build 者( future :) is recursing my http requests,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54633555/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com