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

后端 未结 6 883
清酒与你
清酒与你 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:58

    Use StreamController.isClosed to check if the controller is closed or not, if not closed add data to it.

    if (!_controller.isClosed) 
      _controller.sink.add(...); // safe to add data as _controller isn't closed yet
    

    From Docs:

    Whether the stream controller is closed for adding more events.

    The controller becomes closed by calling the close method. New events cannot be added, by calling add or addError, to a closed controller.

    If the controller is closed, the "done" event might not have been delivered yet, but it has been scheduled, and it is too late to add more events.

提交回复
热议问题