Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

StreamBuilder. Bad state: Stream has already been listened to [duplicate]

Tags:

flutter

I have StreamBuilder

Widget build(BuildContext context) {
  return StreamBuilder(
    initialData: false,
    stream: widget.stream, ...

For initializing widget I call:

_EventSpeakerPager(..., streamController.stream.distinct());

And this produce error "Bad state: Stream has already been listened to." Without distinct() it works, but it's not suitable for me.

I've tried asBroadcastStream() and got the same error

Does anybody know, how can I handle this

P.S. I've already looked into these:

topic1, topic2, topic3 - nothing helps

P.P.S. When I use stream without StreamBuilder - all works fine

void initState() {
super.initState();
widget.stream.listen((bool data) {
  setState(() {
    ...
  });
});

}

like image 594
Andrey Turkovsky Avatar asked Oct 18 '18 13:10

Andrey Turkovsky


People also ask

What is StreamBuilder stream?

A stream builder is a widget that can convert user-defined objects into a stream.

What is Streamcontroller flutter?

A controller with the stream it controls. This controller allows sending data, error and done events on its stream. This class can be used to create a simple stream that others can listen on, and to push events to that stream.

How do I use StreamBuilder?

To use StreamBuilder , you need to call the constructor below. Basically, you need to create a Stream and pass it as the stream argument. Then, you have to pass an AsyncWidgetBuilder which can be used to build the widget based on the snapshots of the Stream .


1 Answers

So, all I've needed to do is replace

final StreamController<bool> streamController = StreamController<bool>();

with final StreamController<bool> streamController = StreamController<bool>.broadcast();

like image 167
Andrey Turkovsky Avatar answered Jan 19 '23 19:01

Andrey Turkovsky