flutter 如果源流仍然是空的,Map会完成这项工作吗?

wqsoz72f  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(85)

在下面的例子中,我希望如果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将返回,但它不以这种方式工作。

62lalag4

62lalag41#

应该是这样的:

if(typeOfThing == nul){
return false
}else{return true}

相关问题