Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bloc: is it possible to yield 2 time the same state?

In the login view, if the user taps on the login button without having inserted his credentials, the LoginFailState is yield and the view reacts to it. If he taps again, this LoginFailstate is yield again, but the view doesn't react to it. So, is there a way to yield more times the same state?

There is some code to better explain my situation:

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  @override
  LoginState get initialState => LoginUninitialized();

  @override
  Stream<LoginState> mapEventToState(LoginEvent event) {
    if (event is loginButtonPressed) {
      yield LoginFailState();
    }
  }

View:

 @override
  Widget build(BuildContext context) {
    return BlocBuilder(
      bloc: _loginBloc,
      builder: (BuildContext context, LoginState state) {
    if (state is LoginFail) {
        print ('Login fail');
    }
    return Column(
          ...
    )
like image 963
Little Monkey Avatar asked Jul 02 '19 10:07

Little Monkey


2 Answers

You can receive an update for the "same" State if you don't extend Equitable, or implement your own '==' logic which makes the two LoginFailStates equal.

The solution is to yield a different State in between, like in the Bloc example.

yield LoginLoading();

It gets called on every login button tap. Felangel's LoginBloc example.

like image 51
zabson Avatar answered Oct 21 '22 15:10

zabson


By default BLoC pattern will not emit state when the same state will be passed one after another. One way to do this is to pass your initial BLoC state after passing LoginFailState.

So after user clicks on the button with wrong credentials passed states will not be:

LoginFailState()
LoginFailState()

but

LoginFailState()
LoginEmptyState()
LoginFailState()
LoginEmptyState()

Which will make UI react to each of them.

But I think that the best and cleanest solution is to pass LoadingState from BLoC before passing LoginFailState().

You can follow the blog post that I have recently written regarding this topic.

like image 35
Kacper Kogut Avatar answered Oct 21 '22 13:10

Kacper Kogut