flutter: Unhandled Exception: Bad state: Cannot add new events after calling close : <p>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 bottom navigation bar and when I change tab and go to my previous tab, it returns this error: </p>

<blockquote>
  <p>Unhandled Exception: Bad state: Cannot add new events after calling
  close.</p>
</blockquote>

<p>I know it is because I am closing the stream and then trying to add to it, but I do not know how to fix it because not disposing the <code>publishsubject</code> will result in <code>memory leak</code>.
here is my Ui code:</p>

<pre><code>class CategoryPage extends StatefulWidget {
  @override
  _CategoryPageState createState() =&gt; _CategoryPageState();
}

class _CategoryPageState extends State&lt;CategoryPage&gt; {
  @override
  void initState() {
    serviceBloc.getAllServices();
    super.initState();
  }

  @override
  void dispose() {
    serviceBloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      stream: serviceBloc.allServices,
      builder: (context, AsyncSnapshot&lt;ServiceModel&gt; snapshot) {
        if (snapshot.hasData) {
          return _homeBody(context, snapshot);
        }
        if (snapshot.hasError) {
          return Center(
            child: Text('Failed to load data'),
          );
        }
        return CircularProgressIndicator();
      },
    );
  }
}

_homeBody(BuildContext context, AsyncSnapshot&lt;ServiceModel&gt; snapshot) {
  return Stack(
      Padding(
          padding: EdgeInsets.only(top: screenAwareSize(400, context)),
          child: _buildCategories(context, snapshot))
    ],
  );
}

_buildCategories(BuildContext context, AsyncSnapshot&lt;ServiceModel&gt; snapshot) {
  return Padding(
    padding: EdgeInsets.symmetric(vertical: 20),
    child: GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 3, crossAxisSpacing: 3.0),
      itemCount: snapshot.data.result.length,
      itemBuilder: (BuildContext context, int index) {
        return InkWell(
          child: CategoryWidget(
            title: snapshot.data.result[index].name,
            icon: Icons.phone_iphone,
          ),
          onTap: () {},
        );
      },
    ),
  );
}
</code></pre>

<p>here is my bloc code:</p>

<pre><code>class ServiceBloc extends MainBloc {
  final _repo = new Repo();
  final PublishSubject&lt;ServiceModel&gt; _serviceController =
      new PublishSubject&lt;ServiceModel&gt;();
  Observable&lt;ServiceModel&gt; get allServices =&gt; _serviceController.stream;
  getAllServices() async {
    appIsLoading();
    ServiceModel movieItem = await _repo.getAllServices();
    _serviceController.sink.add(movieItem);
    appIsNotLoading();
  }

  void dispose() {
    _serviceController.close();
  }
}

ServiceBloc serviceBloc = new ServiceBloc();
</code></pre>

<p>I did not include the repo and API code because it is not in the subject of this error.</p>
