rozerro
rozerro

Reputation: 7216

Will the mapping do the job if the source stream is still empty?

In the following example I expected that if typeOfThingSubject was not still emited with a value than the existing not null things should be returned from else branch. Instead, in the stream builder snapshot.data is 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( ...

In the definition of filteredThings it map over BehaviorSubject<TypeOfThing?> and at the moment of initialization the subject still wasn't emitted with data, so why when the execution in map so named part of the code the else branch doesn't work?

I expected that if stream typeOfThingSubject was still null then the existing iterable will return but it doesn't work this way.

Upvotes: 0

Views: 82

Answers (1)

Adam Basha
Adam Basha

Reputation: 570

It should be like this:

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

Upvotes: 0

Related Questions