我有一个小部件,可以评估流中的数据。我想让此数据显示为反应性:
class BuilderLoading extends StatelessWidget {
@override
Widget build(BuildContext context) {
TenderApiProvider apiProv = Provider.of<TenderApiProvider>(context);
// apiProv.getToken();
return StreamBuilder(
stream: apiProv.resultController,
builder: (BuildContext context, AsyncSnapshot snapshot) {
String tokenLoadingState = '';
String regionsLoadingState = '';
String industryLoadingState = '';
if (snapshot.data is ApiKeyLoadingState) {
switch (snapshot.data) {
case ApiKeyLoadingState.Progress:
tokenLoadingState = "Loading";
break;
case ApiKeyLoadingState.Done:
tokenLoadingState = "Done";
break;
case ApiKeyLoadingState.Error:
tokenLoadingState = "Error";
break;
default:
return Text("Unknown");
}
}
else if (snapshot.data is RegionsLoadingState) {
switch (snapshot.data) {
case RegionsLoadingState.Progress:
regionsLoadingState = "Loading";
break;
case RegionsLoadingState.Done:
regionsLoadingState = "Done";
break;
case RegionsLoadingState.Error:
regionsLoadingState = "Error";
break;
default:
return Text("Unknown");
}
}
else if (snapshot.data is IndustryLoadingState) {
switch (snapshot.data) {
case IndustryLoadingState.Progress:
industryLoadingState = "Loading";
break;
case IndustryLoadingState.Done:
industryLoadingState = "Done";
break;
case IndustryLoadingState.Error:
industryLoadingState = "Error";
break;
default:
return Text("Unknown");
}
}
return Column(
children: <Widget>[
Text("Loading token $tokenLoadingState"),
Text("Loading regions $regionsLoadingState"),
Text("Loading industry $industryLoadingState"),
],
);
});
}
}
但是逻辑上存在问题,我不知道如何解决。得到tokenLoadingState
后,我想显示它的状态,而不是得到regionsLoadingState
,我想一起显示tokenLoadingState
和它。
但是似乎小部件每次都会重绘,而且我只能看到:
Loading token Done
Loading regions
Loading industry
然后:
Loading token
Loading regions Done
Loading industry
然后
Loading token
Loading regions
Loading industry Done
我还需要一种方法来评估所有条件是否都已完成并切换到HomePage();
答案 0 :(得分:0)
如果要在所有条件均已满足的情况下导航到home
,则必须在BLoC内创建另一个状态,因此,很明显,您将首先在条件树中对其进行检查,否则将不会执行。之后:
if(snapshot.data is AllConditionsDone){
SchedulerBinding.instance.addPostFrameCallback((_) {
Navigator.of(context).push('home');
});
}
分享有关逻辑问题的更多信息,以便我们提供帮助。
答案 1 :(得分:0)
由于某种原因,您仅在每个分支中设置tokenLoadingState
,regionsLoadingState
和industryLoadingState
保持初始化为空字符串。
关于您评估多个条件的目标,我建议以下解决方案之一:
StreamBuilder
。