title: Flutter Bloc Builder is not getting called and not updating UI after state change
body: <p>I have a problem with Flutter Bloc package. Main idea is to get list of users from API, I turned of the internet to see how my Error state works, but when try/catch block catches an error my Bloc Builder is not getting rebuilt and my UI isn't changing and I have to do hot restart to see the results.</p>
  
  <p>UserBloc class:</p>
  
  <pre><code>    import 'dart:io';
  
  import 'package:chopper/chopper.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter_bloc/flutter_bloc.dart';
  import 'package:listviewblocflutter/bloc/user/user_event.dart';
  import 'package:listviewblocflutter/bloc/user/user_state.dart';
  import 'package:listviewblocflutter/data/error/network_error.dart';
  import 'package:listviewblocflutter/data/service/user_service.dart';
  import 'package:listviewblocflutter/model/built/built_user.dart';
  import 'package:built_collection/built_collection.dart';
  
  class UserBloc extends Bloc&lt;UserEvent, UserState&gt; {
    UserService _userService;
  
    UserBloc() {
      this._userService = UserService.create();
    }
  
    @override
    UserState get initialState =&gt; UserInitial();
  
    @override
    Stream&lt;UserState&gt; mapEventToState(UserEvent event) async* {
      yield UserLoading();
      if (event is GetAllUsers) {
        print("getallusers");
        try {
          final listOfUsers = await _userService.getAllUsers();
          yield UsersLoaded(listOfUsers.body);
          print("users loaded (userBloc)");
        } catch (_) {
          print("got an error (userBloc)");
          yield UserLoadingError("Can't get all users!");
        }
      } else if (event is GetUserById) {
        // single user event
      }
    }
  }
  </code></pre>
  
  <p>UserEvent class:</p>
  
  <pre><code>import 'package:equatable/equatable.dart';
  
  abstract class UserEvent extends Equatable {
    const UserEvent();
  }
  
  class GetAllUsers extends UserEvent {
    const GetAllUsers();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  
  class GetUserById extends UserEvent {
    final int id;
  
    const GetUserById(this.id);
  
    @override
    List&lt;Object&gt; get props =&gt; [id];
  }
  </code></pre>
  
  <p>UserState class:</p>
  
  <pre><code>import 'package:equatable/equatable.dart';
  import 'package:flutter/cupertino.dart';
  import 'package:listviewblocflutter/model/built/built_user.dart';
  import 'package:built_collection/built_collection.dart';
  
  @immutable
  abstract class UserState extends Equatable {
    const UserState();
  }
  
  class UserInitial extends UserState {
    const UserInitial();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  
  class UserLoading extends UserState {
    const UserLoading();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  
  class UserLoaded extends UserState {
    final BuiltUser user;
  
    const UserLoaded(this.user);
  
    @override
    List&lt;Object&gt; get props =&gt; [user];
  }
  
  class UsersLoaded extends UserState {
    final BuiltList&lt;BuiltUser&gt; listOfUsers;
  
    const UsersLoaded(this.listOfUsers);
  
    @override
    List&lt;Object&gt; get props =&gt; [listOfUsers];
  }
  
  class UserLoadingError extends UserState {
    final String message;
  
    const UserLoadingError(this.message);
  
    @override
    List&lt;Object&gt; get props =&gt; [message];
  }
  
  class UserEmpty extends UserState {
    const UserEmpty();
  
    @override
    List&lt;Object&gt; get props =&gt; [];
  }
  </code></pre>
  
  <p>I wrapped my HomePage with BlocProvider in main.dart file to get an access of my UserBloc in HomePage:</p>
  
  <pre><code>class MyApp extends StatelessWidget {
    // This widget is the root of your application.
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'ListView with BLoC pattern',
        home: SafeArea(
            child: Scaffold(
                body: BlocProvider(
                    create: (context) =&gt; UserBloc(), child: HomePage()))),
      );
    }
  }
  </code></pre>
  
  <p>Tried to make my HomePage class as StateLessWidget, but the problem still occurred, right now my class is StateFulWidget with the same problem. <em>I get AllUsers in initState method</em>:</p>
  
  <pre><code>import 'package:flutter/cupertino.dart';
  import 'package:flutter/material.dart';
  import 'package:flutter_bloc/flutter_bloc.dart';
  import 'package:listviewblocflutter/bloc/bloc.dart';
  import 'package:listviewblocflutter/model/built/built_user.dart';
  import 'package:built_collection/built_collection.dart';
  
  class HomePage extends StatefulWidget {
    @override
    _HomePageState createState() =&gt; _HomePageState();
  }
  
  class _HomePageState extends State&lt;HomePage&gt; {
    @override
    void initState() {
      BlocProvider.of&lt;UserBloc&gt;(context).add(GetAllUsers());
      super.initState();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: AppBar(
          title: Text("Users"),
        ),
        body: Container(
          child: BlocListener&lt;UserBloc, UserState&gt;(
            listener: (context, state) {
              if (state is UserLoadingError) {
                print("showing snackbar (listener)");
                _showNetworkErrorSnackbar(context, state.message);
              }
            },
            child: BlocBuilder&lt;UserBloc, UserState&gt;(
              builder: (context, state) {
                print("bloc builder");
                if (state is UserLoading || state is UserInitial)
                  return _showLoadingStatus();
                else if (state is UsersLoaded)
                  return _buildList(state.listOfUsers);
                else if (state is UserEmpty || state is UserLoadingError) {
                  print("state is empty/error (builder)");
                  return _emptyListLayout();
                }
                return Center();
              },
            ),
          ),
        ),
      );
    }
  
    ListView _buildList(BuiltList&lt;BuiltUser&gt; listOfUsers) {
      return ListView.builder(
          itemCount: listOfUsers.length,
          itemBuilder: (context, index) {
            return Card(
              child: ListTile(
                leading: CircleAvatar(
                  backgroundColor: Colors.redAccent,
                ),
                title: Padding(
                  padding: EdgeInsets.only(left: 10),
                  child: Text(
                    listOfUsers[index].username,
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ),
            );
          });
    }
  
    Widget _showLoadingStatus() {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
  
    Widget _emptyListLayout() {
      return Center(
        child: Text(
          "List is empty ;(",
          textAlign: TextAlign.center,
        ),
      );
    }
  
    void _showNetworkErrorSnackbar(BuildContext context, String message) {
      Scaffold.of(context).showSnackBar(SnackBar(
        content: Text(message),
      ));
    }
  }
  </code></pre>
  
  <p>My goal is to tell the user that list is empty and show a snackbar when UserLoadingError state appears, but I only see a stuck LoadingIndicator
  My "print" debugger in logs says:<br>
  I/flutter (14034): bloc builder<br>
  I/flutter (14034): getallusers<br>
  I/flutter (14034): bloc builder<br>
  I/flutter (14034): got an error (userBloc)<br>
  I/flutter (14034): showing snackbar (listener)<br></p>
  
  <p>Expected results:<br>
  I/flutter (14034): bloc builder<br>
  I/flutter (14034): getallusers<br>
  I/flutter (14034): bloc builder<br>
  I/flutter (14034): got an error (userBloc)<br>
  I/flutter (14034): showing snackbar (listener) /// a snackbar should appear after this message<br>
  I/flutter (14034): bloc builder<br>
  I/flutter (14034): state is empty/error (builder) /// an empty list layout should be created after this message<br></p>
  
  <p>pubspecs.yaml fragment:<br>
  dependencies:<br>
    flutter:<br>
      sdk: flutter<br>
    cupertino_icons: ^0.1.3<br>
    chopper: ^3.0.2<br>
    equatable: ^1.1.1<br>
    flutter_bloc: ^4.0.0<br>
    provider: ^4.1.1<br>
    built_value: ^7.1.0<br></p>
  
  <p>dev_dependencies:<br>
    chopper_generator: ^3.0.4<br>
    build_runner: ^1.10.0<br>
    built_value_generator: ^7.1.0<br>
    flutter_test:<br>
      sdk: flutter<br></p>
  
  <p>I could upload my UserService class where I get users from API using Chopper, let me know if it's needed.</p>