在一个小部件中是否可以有多个StreamBuilder
?
我已经关注了小部件:
class BuilderLoading extends StatelessWidget {
@override
Widget build(BuildContext context) {
TenderApiProvider apiProv = Provider.of<TenderApiProvider>(context);
// apiProv.getToken();
return StreamBuilder<ApiKeyLoadingState>(
stream: apiProv.streamApiKeyController.stream, // here
builder: (BuildContext context, AsyncSnapshot<ApiKeyLoadingState> snapshot)
{
switch (apiProv.apiKeyLoadingState) {
case ApiKeyLoadingState.Progress:
return Text("Retreiving key");
case ApiKeyLoadingState.Done:
return HomePage();
case ApiKeyLoadingState.Error:
return Text("Key Got Error");
break;
default:
return Text("Unknown");
}
}
);
}
}
我在这里检查streamApiKeyController
。但我想在此窗口小部件中显示另一个流的状态。喜欢:
streamApiKeyController
,streamRegionsLoadingController
,streamIndustryLoadingController
。我该怎么办?
我的意思是:
return (
builder: (BuildContext context, AsyncSnapshot<ApiKeyLoadingState> snapshot)
{
// ... streamApiKeyController
}
builder: (BuildContext context, AsyncSnapshot<regionsLoadingState> snapshot)
{
// ... streamRegionsLoadingController
}
);
要获取列表:
领取钥匙
RegionsLoading
答案 0 :(得分:1)
您可以像这样嵌套StreamBuilder
,只需从第一个构建器的第二个流中返回另一个StreamBuilder
即可:
class BuilderLoading extends StatelessWidget {
@override
Widget build(BuildContext context) {
TenderApiProvider apiProv = Provider.of<TenderApiProvider>(context);
// apiProv.getToken();
return StreamBuilder<ApiKeyLoadingState>(
stream: apiProv.streamApiKeyController.stream, // here
builder: (BuildContext context, AsyncSnapshot<ApiKeyLoadingState> apiKeySnapshot)
{
return StreamBuilder<regionsLoadingState>(
stream: streamRegionsLoadingController,
builder: (BuildContext context, AsyncSnaptshot<regionsLoadingState> regioSnapshot)
{
if (!regioSnapshot.hasData) return Text("Regiodata not loaded");
switch (apiProv.apiKeyLoadingState) {
case ApiKeyLoadingState.Progress:
return Text("Retreiving key");
case ApiKeyLoadingState.Done:
return HomePage();
case ApiKeyLoadingState.Error:
return Text("Key Got Error");
default:
return Text("Unknown");
}
}
);
}
);
}
}
当然,您需要在内部构建器中定义自己的自定义逻辑,我只是出于演示目的添加了一些“虚拟”逻辑。
答案 1 :(得分:0)
您可以嵌套StreamBuilders:
class BuilderLoading extends StatelessWidget {
@override
Widget build(BuildContext context) {
TenderApiProvider apiProv = Provider.of<TenderApiProvider>(context);
// apiProv.getToken();
return StreamBuilder<ApiKeyLoadingState>(
stream: apiProv.streamApiKeyController.stream,
builder: (BuildContext context, AsyncSnapshot<ApiKeyLoadingState> snapshotAPIKey){
return StreamBuilder<ApiKeyLoadingState>(
stream: streamRegionsLoadingController,
builder: (BuildContext context, AsyncSnapshot<ApiKeyLoadingState> snapshotRegionsLoading)
{
/// Do Stuff here
}
);
}
);
}
}