为了避免我的FutureBuilder
被一次又一次地调用(参见this),我习惯在initState
中调用我的futures,它一直工作得很好,直到我不得不使用嵌套的futures。
基本上,我希望第二个未来只有在第一个未来的connectionState
变成done
之后才被调用。
下面是我的代码来进一步解释-
FutureBuilder(
future: _future1,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return snapshot.connectionState != ConnectionState.done
? CircularProgressIndicator()
: snapshot.data != 200
? SomeWidget()
: FutureBuilder(
future: _future2,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return snapshot?.connectionState == ConnectionState.done
? Text('')
: CircularProgressIndicator();
});
},
);
字符串
所以基本上,如果从_future1
接收的数据不是200,我根本不希望_future2
被调用。换句话说,我想根据_future1
的结果来决定是否应该调用_future2
。
2条答案
按热度按时间mv1qrgav1#
在
initState
中,我这样做-字符串
然后我写了另一个函数来决定我的小部件取决于我收到的代码-
型
最后,我的构建代码看起来像这样-
型
nxowjjhe2#
将内部的
FutureBuilder
提取到一个单独的有状态小部件中,只有当外部条件这样说时才调用它(显示其他东西,否则可能只是一个空的Container
)。然后它就可以拥有自己的内部逻辑和自己的未来。即使第二个未来出现得很晚,这个解决方案也能工作,不是直接在页面的开头,而是响应用户输入。