flutter: Unhandled Exception: Bad state: Cannot add new events after calling close

后端 未结 6 858
清酒与你
清酒与你 2021-01-01 12:01

I am trying to use the bloc pattern to manage data from an API and show them in my widget. I am able to fetch data from API and process it and show it, but I am using a bott

6条回答
  •  执笔经年
    2021-01-01 12:53

    If the error is actually caused by the code you posted, I'd just add a check to ensure no new events are added after dispose() was called.

    class ServiceBloc extends MainBloc {
      final _repo = new Repo();
      final PublishSubject _serviceController =
          new PublishSubject();
      Observable get allServices => _serviceController.stream;
      getAllServices() async {
        // do nothing if already disposed
        if(_isDisposed) {
          return;
        }
        appIsLoading();
        ServiceModel movieItem = await _repo.getAllServices();
        _serviceController.sink.add(movieItem);
        appIsNotLoading();
      }
    
      bool _isDisposed = false;
      void dispose() {
        _serviceController.close();
        _isDisposed = true;
      }
    }
    
    ServiceBloc serviceBloc = new ServiceBloc();
    

提交回复
热议问题