I’m building a simple signup process with Firebase Auth and Bloc. When the user creates an account it triggers an event of the SignUpBloc called SignUpRequired. On that event, an account on Firebase is created and emits the state SignUpSuccess (and there are also SignUpProcess and SingUpError). I’m using a simple bloc observer to verify every step. It works totally fine, however, the BlocListener doesn’t listen to any change of the bloc state.
Here is the Widget with the BlocListener:
BlocProvider<SignUpBloc>(
create: (context) => SignUpBloc(
userRepository:
context.read<AuthenticationBloc>().userRepository),
child: BlocListener<SignUpBloc, SignUpState>(
listener: (context, state) {
if (state is SignUpSuccess) {
print('success');
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(
builder: (context) => const Dashboard(),
),
(route) => false,
);
} else if (state is SignUpProcess) {
print('process');
} else {
print('failure');
}
},
child: child,
),
)
Here is the SignUpBloc:
class SignUpBloc extends Bloc<SignUpEvent, SignUpState> {
final UserRepository _userRepository;
SignUpBloc({required UserRepository userRepository})
: _userRepository = userRepository,
super(SignUpInitial()) {
on<SignUpRequired>((event, emit) async {
emit(SignUpProcess());
try {
MyUser user = await _userRepository.signUp(event.email, event.password);
await _userRepository.setUserData(user);
emit(SignUpSuccess());
} catch (e) {
emit(SignUpFailure());
}
});
}
}
And lastly, the debug console:
[log] onEvent -- bloc: SignUpBloc, event: SignUpRequired()
[log] onChange -- bloc: SignUpBloc, change: Change { currentState: SignUpSuccess(), nextState: SignUpProcess() }
[log] onTransition -- bloc: SignUpBloc, transition: Transition { currentState: SignUpSuccess(), event: SignUpRequired(), nextState: SignUpProcess() }
[log] onEvent -- bloc: AuthenticationBloc, event: AuthenticationUserChanged()
[log] onTransition -- bloc: AuthenticationBloc, transition: Transition { currentState: AuthenticationState(AuthenticationStatus.authenticated, User(displayName: null, email: [email protected], isEmailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2024-08-15 15:18:20.693Z, lastSignInTime: 2024-08-15 15:18:20.693Z), phoneNumber: null, photoURL: null, providerData, [UserInfo(displayName: null, email: [email protected], phoneNumber: null, photoURL: null, providerId: password, uid: [email protected])], refreshToken: AMf-vBxVhZkUQweZzg-O_x_AkjbOROfJ72ty4VtCuBMPSrwBDDpdRnMa0oqejXDZU37G0ur-u4aXRvjv50DWELi6MF_8AP6_PdAUgS0w8uQxad5zxg9CiQ9XxMbqfEdfSAq1LVmEFIM00_CttNpu_9XwlP3ky1P6K3Ys0V0JKW5MvbStataPqO5zJ6V_iBnx2sWxgnyNLPNJn-O4CfLBXRn5UViiPLMy8A, tenantId: null, uid: cSEG28J8wUX7lSkXuvCJl2Slu983)), event: AuthenticationUserChanged(), nextState: AuthenticationState(AuthenticationStatus.authenticated, User(displayName: null, email: [email protected], isEmailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2024-08-15 15:21:29.926Z, lastSignInTime: 2024-08-15 15:21:29.926Z), phoneNumber: null, photoURL: null, providerData, [UserInfo(displayName: null, email: [email protected], phoneNumber: null, photoURL: null, providerId: password, uid: [email protected])], refreshToken: AMf-vBynmyZHJJAefSPj6FO5P5-IC3pN8CJOuK0aEoD-XQWrhSSYetE_BYU57XlhBa5nZTHVLn9OooyUuy9MrHhP7kXBygRaPHnktobnGLFTv9z1BlgminoQ5KpMDY21mJseyb5oW-GCERcbNg05VU99k1z2D8uJZVNsaWVe9T7z1WN2lbZxOJ9YNZbujsp26v1ekj8By2_2ZHQNcwmoYQT6LwYApkd0CQ, tenantId: null, uid: f6ECNkviyPRL8Cr5QgXqOadyOMj1)) }
[log] onChange -- bloc: AuthenticationBloc, change: Change { currentState: AuthenticationState(AuthenticationStatus.authenticated, User(displayName: null, email: [email protected], isEmailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2024-08-15 15:18:20.693Z, lastSignInTime: 2024-08-15 15:18:20.693Z), phoneNumber: null, photoURL: null, providerData, [UserInfo(displayName: null, email: [email protected], phoneNumber: null, photoURL: null, providerId: password, uid: [email protected])], refreshToken: AMf-vBxVhZkUQweZzg-O_x_AkjbOROfJ72ty4VtCuBMPSrwBDDpdRnMa0oqejXDZU37G0ur-u4aXRvjv50DWELi6MF_8AP6_PdAUgS0w8uQxad5zxg9CiQ9XxMbqfEdfSAq1LVmEFIM00_CttNpu_9XwlP3ky1P6K3Ys0V0JKW5MvbStataPqO5zJ6V_iBnx2sWxgnyNLPNJn-O4CfLBXRn5UViiPLMy8A, tenantId: null, uid: cSEG28J8wUX7lSkXuvCJl2Slu983)), nextState: AuthenticationState(AuthenticationStatus.authenticated, User(displayName: null, email: [email protected], isEmailVerified: false, isAnonymous: false, metadata: UserMetadata(creationTime: 2024-08-15 15:21:29.926Z, lastSignInTime: 2024-08-15 15:21:29.926Z), phoneNumber: null, photoURL: null, providerData, [UserInfo(displayName: null, email: [email protected], phoneNumber: null, photoURL: null, providerId: password, uid: [email protected])], refreshToken: AMf-vBynmyZHJJAefSPj6FO5P5-IC3pN8CJOuK0aEoD-XQWrhSSYetE_BYU57XlhBa5nZTHVLn9OooyUuy9MrHhP7kXBygRaPHnktobnGLFTv9z1BlgminoQ5KpMDY21mJseyb5oW-GCERcbNg05VU99k1z2D8uJZVNsaWVe9T7z1WN2lbZxOJ9YNZbujsp26v1ekj8By2_2ZHQNcwmoYQT6LwYApkd0CQ, tenantId: null, uid: f6ECNkviyPRL8Cr5QgXqOadyOMj1)) }
[log] onChange -- bloc: SignUpBloc, change: Change { currentState: SignUpProcess(), nextState: SignUpSuccess() }
[log] onTransition -- bloc: SignUpBloc, transition: Transition { currentState: SignUpProcess(), event: SignUpRequired(), nextState: SignUpSuccess() }
The last issue i’ve found is that the primary state of SignUpBloc is SignUpSuccess and not SignUpInitial. I’m not sure if this could cause any problem to the code. Let me know 🙂