skip to Main Content

How to fix this error: StateError (Bad state: add(SignUp Button Pressed) was called without a registered event handler. Make sure to register a handler via on((event, emit) {…}))

SignUpBloc:

import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import '../../domain/repositories/user_repository.dart';

part 'sign_up_event.dart';
part 'sign_up_state.dart';

class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
  final UserRepository userRepository;

  SignUpBloc({required this.userRepository}) : super(SignUpInitial());

  @override
  Stream<SignUpState> mapEventToState(SignUpEvent event) async* {
    if (event is SignUpButtonPressed) {
      yield SignUpLoading();

      try {
        final email = event.email;
        final password = event.password;

        
        final result = await userRepository.registerUser(email, password);
        
        yield result.fold(
          (failure) => SignUpError(message: failure.toString()),
          (user) => SignUpSuccess(email: user.email),
        );
      } catch (error) {
        yield SignUpError(message: error.toString());
      }
    }
  }
}

2

Answers


  1. I have another way to use bloc based on your error handler via on((event, emit) {...})).

    import 'package:bloc/bloc.dart';
    import 'package:equatable/equatable.dart';
    import '../../domain/repositories/user_repository.dart';
    
    part 'sign_up_event.dart';
    part 'sign_up_state.dart';
    
    class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
      final UserRepository userRepository;
    
      SignUpBloc({required this.userRepository}) : super(SignUpInitial());
    
      on<SignUpEvent>((event, emit) async {
        emit(SignUpLoading());
    
        try {
          final email = event.email;
          final password = event.password;
    
          
          final result = await userRepository.registerUser(email, password);
          
          await result.fold(
            (failure) async => SignUpError(message: failure.toString()),
            (user) async => SignUpSuccess(email: user.email),
          );
        } catch (error) {
          emit(SignUpError(message: error.toString()));
        }
      });
    }
    

    Please understand the concept of bloc. Bloc has 3 files, event || bloc || state.

    1. Event is for any input to the bloc.
    2. Bloc is anything do you want to do with the input data variable from
      event.
    3. State is the output of bloc, always use emit().
    Login or Signup to reply.
  2. The mapEventToState method is commonly used in older versions of the Flutter Bloc library.

    For new version

    class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
      SignUpBloc({required this.userRepository}) : super(SignUpInitial()){
        on<SignUpButtonPressed>(_handleSignUpButtonPressed);
      };
    
      final UserRepository userRepository;
      
      Future _handleSignUpButtonPressed(SignUpButtonPressed event, Emitter<SignUpState> emit) async {
        emit(SignUpLoading());
        //...
        //your logic
      }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search