【发布时间】:2021-12-05 22:44:38
【问题描述】:
我将使用比我的项目更简化的版本,但如有必要,请随时询问更多代码。这可能更像是一个架构问题。
我的应用程序中有一个服务(接口)FooService,它公开了一个流 fooStream。我作为集团类MyBloc 的成员拥有这项服务。
现在 MyBlocState 块的状态包含 isProcessing 以指示块是否正在处理,并且它还包含 fooStream 字段,它将服务的 fooStream 版本暴露给 UI 层(所以有 3 层: service => bloc => UI)。
现在的问题是,当我想在我的 UI 中收听此服务流时,我不得不使用 StreamBuilder,它本身将被包裹在 BlocBuilder 中。这在大型应用程序中会变得非常混乱。那么在欧盟国家内部代表fooStream 的最佳方式是什么?每当fooStream 事件到达时,我可以只收听集团内的流并向集团添加一个事件吗?如果状态将以混乱的方式更新并且无法再跟踪状态的变化,这不是一个不好的副作用吗?
示例代码:
class FooService{
Stream fooStream;// the stream is exposed here
...
}
class MyBloc extends ... {
FooService myService = ...;//the service is used here as a member
...
}
class MyBlocState extends ... {
Stream? fooStream;//this value will hold the stream exposed from the service and it may be initialized in an init event in the bloc for example
...
}
现在在 UI 中我必须做这个丑陋的事情(丑陋,因为现在我正在从两个地方听状态,从集团和流,在我看来,这违背了集团作为唯一的目的在 UI/app 中更改状态和维护状态的入口点):
Widget build(BuildContext context){
return BlocBuilder<MyBloc,MyBlocState>(
builder: (context, myBlocState) {
if(myBlocState is Loading){
return CircularProgressIndicator();
}
if(myBlocState is Error){
return ErrorWidget();
}
if(myBlocState is SomeState){
return StreamBuilder(//I need to wrap the StreamBuilder inside the BlocBuilder because the stream will be null when the app launches but then it will have a value which is fooStream when I call init in the bloc
stream: myBlocState.fooStream,
builder: (context, snapshot) {
if(snapshot.hasData){
return SomeWidget();
}
else{
return SomeOtherWidget();
}
},
);
}
},
)
}
【问题讨论】:
标签: java ios flutter dart architecture