Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Stream for counter maybe too complicated for beginning. #2

Open
killvung opened this issue Aug 14, 2022 · 0 comments
Open

Using Stream for counter maybe too complicated for beginning. #2

killvung opened this issue Aug 14, 2022 · 0 comments

Comments

@killvung
Copy link

So using StreamController to store the count state and display it with StreamBuilder and AsyncSnapshot in the UI are really complicated for Flutter beginner without BloC experience. For me I would simply storing the count state as an int and registering events under the bloc class with on instead of mapping them, then display it with context
For example

class CounterBloc extends Bloc<CounterEvent, CounterState> {
  CounterBloc() : super(const Counter(0)) {
    on<CounterIncrementPressed>(
        (event, emit) => emit(Counter(state.count + 1)));
    on<CounterDecrementPressed>(
        (event, emit) => emit(Counter(state.count - 1)));

    on<CounterResetPressed>((event, emit) => emit(const Counter(0)));
  }
}

Then in UI

body: Center(
        child: BlocBuilder<CounterBloc, int>(
          builder: (context, count) {
            return Text('$count', style: textTheme.headline2);
          },
        ),
      ),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant