Initial value with StreamController without RxDart

Issue

I’m using StreamControllers with Flutter. I have a model with some default values. From the widgets where I’m listening to the stream I want to supply some of those default values. I can see I can set an initial value on the StreamBuilder, but I want to use data from the model inside the bloc as initial data. So as soon as someone is using the snapshot data they get the default values. I’ve seen RxDart has a seed value, just wondering if this is possible without replacing with RxDart?

Solution

What you are looking for is StreamController#add method,

Sends a data event.

Listeners receive this event in a later microtask.

Note that a synchronous controller (created by passing true to the
sync parameter of the StreamController constructor) delivers events
immediately. Since this behavior violates the contract mentioned here,
synchronous controllers should only be used as described in the
documentation to ensure that the delivered events always appear as if
they were delivered in a separate microtask.

happy fluttering

Answered By – pskink

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *