在下面的例子中,我希望如果typeOfThingSubject
没有被释放一个值,那么现有的非空things
应该从else
分支返回。相反,在流构建器中,snapshot.data
为null。
// in bloc constructor
final typeOfThingSubject = BehaviorSubject<TypeOfThing?>();
// at this point the above subject still wasn't emitted with data
final filteredThings = typeOfThingSubject
.map<Iterable<Thing>>((value) {
if (value != null) {
return <Thing>[...];
} else {
// obviously this branch should be executed but that doesn't happen
// when typeOfThingSubject wasn't emitted with an event yet
// and truly the `typeOfThingSubject` is just declared at this point
return things; // existing iterable
}
});
return Bloc._( ...
things: filteredThings, // this is the same stream `things` as in the builder below
);
// in view
StreamBuilder<Iterable<Thing>>(
stream: bloc.things,
builder: (context, snapshot) {
// at this point `snapshot.data` is null
final things = snapshot.data ?? [];
return ListView.builder( ...
在filteredThings
的定义中,它Map到BehaviorSubject<TypeOfThing?>
上,在初始化的时候,主题仍然没有发送数据,那么为什么当map
中的代码命名部分执行else
分支不起作用呢?
我期望如果流typeOfThingSubject
仍然是null
,那么现有的iterable
将返回,但它不以这种方式工作。
1条答案
按热度按时间62lalag41#
应该是这样的: