title: How can I change my code to allow me to correctly use BLoC without setState?
body: <p><strong>Introduction:</strong> I am encountering an unexpected problem in my code when using BLoC, which I don't know how to fix, and I was looking for a solution.</p>
  <p>When I use the code below, an object adds to the ListView.builder whenever I click on the ListTile as expected. However, This addition only happens once and when I continue to press on the ListTile to add more items to the list, the state is not updated (which is verifiable by pressing the Hot Reload button, which then updates the table with the items).</p>
  <p><strong>Problem:</strong> I have converted the MyHomePage widget to a StatefulWidget and wrapped the call to my bloc in a setState method (uncomment the commented section of the code to see that it works as expected). Now, as I understand it, I should be able to use BLoC for my state management needs and not need to use a StatefulWidget.</p>
  <p><strong>Questions:</strong></p>
  <ol>
  <li>Why is my code not working correctly?</li>
  <li>How can I change my code to fix my problem?</li>
  </ol>
  <p><strong>Code:</strong>
  Here is the complete minimum code, which reflects the problem that I am having:</p>
  <pre><code>import 'package:flutter/material.dart';
  import 'package:flutter_bloc/flutter_bloc.dart';
  
  main() {
    runApp(const MyApp());
  }
  
  class MyApp extends StatelessWidget {
    const MyApp({super.key});
  
    @override
    Widget build(BuildContext context) {
      return BlocProvider(
        create: (context) =&gt; CollectionBloc(),
        child: const MaterialApp(
          home: MyHomePage(),
        ),
      );
    }
  }
  
  class MyHomePage extends StatefulWidget {
    const MyHomePage({super.key});
  
    @override
    State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
  }
  
  class _MyHomePageState extends State&lt;MyHomePage&gt; {
    @override
    Widget build(BuildContext context) {
      return BlocBuilder&lt;CollectionBloc, Collection&gt;(
        builder: (context, state) {
          return Scaffold(
            appBar: AppBar(
              title: const Text('Flutter Demo Home Page'),
            ),
            body: ListView.builder(
              itemCount: state.hierarchy(state).length,
              itemBuilder: (context, index) {
                return ListTile(
                  onTap: () {
                    var showType = state.hierarchy(state)[index].showType;
                    // TODO: setState function is here.
                    setState(() {
                      if (showType == ShowType.collection) {
                        BlocProvider.of&lt;CollectionBloc&gt;(context).add(AddSeries(
                            series: Collection(
                          name:
                              &quot;Series ${state.getChildrenOfNode(state.hierarchy(state)[index]).length + 1}&quot;,
                          showType: ShowType.series,
                          children: [],
                        )));
                        BlocProvider.of&lt;CollectionBloc&gt;(context).add(UpdateBloc(collection: state));
                      }
                    
                    if (showType == ShowType.series) {
                      BlocProvider.of&lt;CollectionBloc&gt;(context).add(AddSeason(
                          series: state.hierarchy(state)[index],
                          season: Collection(
                            name:
                                'Season ${state.getChildrenOfNode(state.hierarchy(state)[index]).length + 1}',
                            showType: ShowType.season,
                            children: [],
                          )));
                    }
                    if (showType == ShowType.season) {
                      BlocProvider.of&lt;CollectionBloc&gt;(context).add(AddEpisode(
                          season: state.hierarchy(state)[index],
                          episode: Collection(
                            name:
                                &quot;Episode ${state.getChildrenOfNode(state.hierarchy(state)[index]).length + 1}&quot;,
                            showType: ShowType.episode,
                            children: [],
                          )));
                    }
                    });
                  },
                  leading: Card(
                    child: TextWidget(name: state.hierarchy(state)[index].name),
                  ),
                );
              },
            ),
            floatingActionButton: FloatingActionButton(
              child: const Text('to json'),
              onPressed: () {
                var toJson = state.toJson();
                var fromJson = Collection.fromJson(toJson);
                print(fromJson);
              },
            ),
          );
        },
      );
    }
  }
  
  class TextWidget extends StatelessWidget {
    const TextWidget({super.key, required this.name});
    final String name;
  
    @override
    Widget build(BuildContext context) {
      return Text(name);
    }
  }
  
  /// BLoC
  
  class InitialState extends Collection {
    InitialState(collection)
        : super(
            name: collection.name,
            showType: collection.showType,
            children: collection.children,
          );
  }
  
  abstract class BLoCEvents {}
  
  class AddSeries extends BLoCEvents {
    AddSeries({required this.series});
    final Collection series;
  }
  
  class AddSeason extends BLoCEvents {
    AddSeason({required this.series, required this.season});
    final Collection series;
    final Collection season;
  }
  
  class AddEpisode extends BLoCEvents {
    AddEpisode({required this.season, required this.episode});
    final Collection season;
    final Collection episode;
  }
  
  class UpdateBloc extends BLoCEvents {
    UpdateBloc({required this.collection});
    final Collection collection;
  }
  
  class CollectionBloc extends Bloc&lt;BLoCEvents, InitialState&gt; {
    CollectionBloc()
        : super(InitialState(Collection(
              name: 'Collection', showType: ShowType.collection, children: []))) {
      on&lt;AddSeries&gt;(
          ((event, emit) =&gt; emit(state..addSeries(series: event.series))));
      on&lt;AddSeason&gt;(((event, emit) =&gt;
          emit(state..addSeason(series: event.series, season: event.season))));
      on&lt;AddEpisode&gt;(((event, emit) =&gt;
          emit(state..addEpisode(season: event.season, episode: event.episode))));
      ///todo: update bloc here.
      on&lt;UpdateBloc&gt;(((event, emit) =&gt; print(state.toJson())));
    }
  }
  
  /// Model
  
  enum ShowType { collection, series, season, episode }
  
  class Collection {
    Collection(
        {required this.name, required this.showType, required this.children});
    final String name;
    final ShowType showType;
    List&lt;Collection&gt; children = [];
  
    void addSeries({required Collection series}) =&gt; children.add(series);
  
    void addSeason({required Collection series, required Collection season}) =&gt;
        series.children.add(season);
  
    void addEpisode({required Collection season, required Collection episode}) =&gt;
        season.children.add(episode);
  
    List&lt;Collection&gt; hierarchy(Collection node) {
      List&lt;Collection&gt; list = [];
      list.add(node);
      for (Collection child in node.children) {
        list.addAll(hierarchy(child));
      }
      return list;
    }
  
    List&lt;Collection&gt; getChildrenOfNode(Collection node) {
      List&lt;Collection&gt; list = [];
      for (Collection child in node.children) {
        list.add(child);
      }
      return list;
    }
  
    toJson() {
      return {
        'name': name,
        'showType': showType,
        'children': children.map((child) =&gt; child.toJson()).toList(),
      };
    }
  
    factory Collection.fromJson(Map&lt;String, dynamic&gt; json) {
      return Collection(
          name: json['name'],
          showType: json['showType'],
          children: json['children']
              .map&lt;Collection&gt;((child) =&gt; Collection.fromJson(child))
              .toList());
    }
  
    @override
    String toString() {
      return 'Collection{name: $name, showType: $showType, children: $children}';
    }
  }
  </code></pre>