Flutter : Bad state: Stream has already been listened to

前端 未结 11 1296
灰色年华
灰色年华 2020-12-03 04:21

    class MyPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Defaul         


        
相关标签:
11条回答
  • 2020-12-03 04:36

    In my case, I was getting this error because the same line of code myStream.listen() was being called twice in the same widget on the same stream. Apparently this is not allowed!

    UPDATE: If you intend to subscribe to the same stream more than once, you may want to choose behavior subject instead:

    final _myController = BehaviorSubject<String>();
    // use myStream.listen((latestEvent) {// run code when event is received});
    Stream<String> get myStream =>
          _myController.stream;
    // use myStreamInputSink.add('new event'); to trigger events
    Sink<String> get mySteamInputSink => _myController.sink;
    

    OR If a widget subscribes to a stream, and this widget is destroyed then redrawn which results for its new instance to subscribe to the same listener a second time, it's best to reset on widget dispose:

    _flush() {
      _myController.close();
      _myController = StreamController<String>();
    }
    

    Old Answer:

    What fixed it for me is to both create a my stream controller as a broadcast stream controller:

    var myStreamController = StreamController<bool>.broadcast();
    

    AND

    use stream as a broadcast stream:

    myStreamController.stream.asBroadcastStream().listen(onData);
    
    0 讨论(0)
  • 2020-12-03 04:42

    For those of you running into this while doing Future.asStream(), you'll need Future.asStream().shareReplay(maxSize: 1) to make it a broadcast/hot stream.

    0 讨论(0)
  • 2020-12-03 04:44

    The most common form of Stream can be listened only once at a time. If you try to add multiple listeners, it will throw

    Bad state: Stream has already been listened to

    To prevent this error, expose a broadcast Stream. You can convert your stream to a broadcast using myStream.asBroadcastStream

    This needs to be done inside your class that expose Stream. Not as parameter of StreamBuilder. Since asBroadcastStream internally listen to the original stream to generate the broadcast one, this imply you can't call this method twice on the same stream.

    0 讨论(0)
  • 2020-12-03 04:44

    You could use broadcast, which allows to listen stream more than once, but it also prevents from listening past events:

    Broadcast streams do not buffer events when there is no listener.

    A better option is to use BehaviorSubject from rxdart package class as StreamController. BehaviorSubject is:

    A special StreamController that captures the latest item that has been added to the controller, and emits that as the first item to any new listener.

    The usage is as simple as:

    StreamController<...> _controller = BehaviorSubject();
    
    0 讨论(0)
  • 2020-12-03 04:46

    In my case I was Using the Package Connectivity while on flutter web. Commenting all Connectivity calls solved the issue.

    I'm now just using Connectivity while only on Android/iOS.

    So maybe check your Packages im you are using some packages that have some issues on Web in case you are developing for web.

    Hopefully I could help someone with this Information.

    0 讨论(0)
  • 2020-12-03 04:51

    For me defining my stream as a global variable worked

    Stream infostream (was inside the ...State in a stateful widget i defined it outside the widget and it worked

    (not sure if the best solution but give it a try)

    0 讨论(0)
提交回复
热议问题