Flutter: Stream<Null> is allowed?

2,383

Solution 1

Null as generic type argument was used before void was supported and means in this case that only the occurence of the even is meaningful, but the event value is not.

With Null the value null is the only valid event value. With void a callback function can be passed that does not take any parameter.

Solution 2

That's a hack around the fact that sinks require an argument.

Some peoples use streams as a flux of events instead of a value changing over time, but the class isn't designed with this in mind. They typically try to represent the following method as a stream:

void onClick();
Share:
2,383
Little Monkey
Author by

Little Monkey

I'm just a little monkey!

Updated on December 07, 2022

Comments

  • Little Monkey
    Little Monkey over 1 year

    Sometimes, when I´m watching some projects that work with streams, I see something like:

    final controller = StreamController<Null>();
    

    and then:

    controller.sink.add(null);
    

    So, is allowed to pass null in streams? Why?

  • Little Monkey
    Little Monkey over 5 years
    So, basically it´s allowed, even if streams are not designed to be used like this.
  • altair
    altair over 2 years
    Is there any other way to implement flux of events?