Flutter Bloc How to emit inside a listener


I have wanted to set up authentication using Firebase. I have this auth repository that has this method that gets the current user.

Stream<User?> get user => _firebaseAuth.userChanges();

Inside my bloc, I Have this constructor.

class AuthBloc extends Bloc<AuthEvent, AuthState> {
  final AuthRepository _authRepository;
  late StreamSubscription<User?> _authSubscription;

  AuthBloc(AuthRepository authRepository)
      : _authRepository = authRepository,
        super(const AuthState.initial()) {

  void _onUserChanged(AuthStarted event, Emitter<AuthState> emit) {
    _authSubscription = _authRepository.user.listen((user) async {
      if (user != null) {
      } else {
        const AuthState.unauthenticated();

When my app starts, I’m calling this on my main class.

            create: (context) => AuthBloc(context.read<AuthRepository>())
              ..add(const AuthEvent.started()),

This is how my state looks like

part of 'auth_bloc.dart';

class AuthState with _$AuthState {
  const factory AuthState.initial() = _initial;
  const factory AuthState.authenticated(User user) = _Authenticated;
  const factory AuthState.unauthenticated() = _Unauthenticated;

Now I have this on my UI depending on the state of my app. I want to render different views.

    return state.when(
      initial: () => _buildInitial(context),
      authenticated: (user) => _buildAuthenticated(),
      unauthenticated: () => _buildUnauthenticated(),

I’m getting the following error on my bloc.

enter image description here

This line right here is triggering the error.

enter image description here

I’m using the freezed package to generate Union, and using Bloc 8.0.


I have a solution/workaround for this case.
Let’s make an (for example) AuthEvent.onUserDataUpdated(User) event, in the stream listener you have to call add() with this event and create a handler for it (on<…>(…)) to emit new AuthState.

Answered By – jkirtyan

Answer Checked By – Terry (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *