zoukankan      html  css  js  c++  java
  • Bad state: Stream has already been listened to.

    https://stackoverflow.com/questions/51396769/flutter-bad-state-stream-has-already-been-listened-to

    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.

    Thank you Rémi Rousselet for your response. I exposed the broadcast stream by simply calling myStream.asBroadcastStream. But i am getting snapshot.hasData as null and the progressbar keeps moving as the streambuilder might not able to fetch the data. – Mercy peka J

    I have had the same issue when I used a result of Observable.combineLatest2 for StreamBuilder into Drawer:

    flutter: Bad state: Stream has already been listened to.

    As for me, the best solution has added the result of this combine to new BehaviorSubject and listen new one.

    Don't forget to listen old one !!!

    class VisitsBlocc extends Object {
        Map<Visit, Location> visitAndLocation;
    
        VisitsBloc() {
            visitAndLocations.listen((data) {
                visitAndLocation = data;
            });
        }
    
        final _newOne = new BehaviorSubject<Map<Visit, Location>>();
    
        Stream<Map<Visit, Location>> get visitAndLocations => Observable.combineLatest2(_visits.stream, _locations.stream, (List<vis.Visit> visits, Map<int, Location> locations) {
            Map<vis.Visit, Location> result = {};
    
            visits.forEach((visit) {
                if (locations.containsKey(visit.skuLocationId)) {
                    result[visit] = locations[visit.skuLocationId];
                }
            });
    
            if (result.isNotEmpty) {
                _newOne.add(result);
            }
        });
    }



    I didn't use .broadcast because it slowed my UI.
  • 相关阅读:
    不怕路长,只怕心老——走在IT行业的路上
    python中 r'', b'', u'', f'' 的含义
    WSGI接口
    HTTP协议简介
    Flask中的Session
    一个 android 开机自动启动功能的例子
    遍历 JObject各属性(CSharp2)
    ASP.NET 伪随机数函数避免重复一例
    浏览器环境下 ES6 的 export, import 的用法举例
    在浏览器环境使用js库(不用require功能)
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10822287.html
Copyright © 2011-2022 走看看