Flutter : Bad state: Stream has already been listened to

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

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


        
11条回答
  •  借酒劲吻你
    2020-12-03 04:58

    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 VisitsBloc extends Object {
        Map visitAndLocation;
    
        VisitsBloc() {
            visitAndLocations.listen((data) {
                visitAndLocation = data;
            });
        }
    
        final _newOne = new BehaviorSubject>();
    
        Stream> get visitAndLocations => Observable.combineLatest2(_visits.stream, _locations.stream, (List visits, Map locations) {
            Map 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.

提交回复
热议问题