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

Add optional condition to BlocListener #406

merged 1 commit into from Jul 15, 2019


Copy link

commented Jul 14, 2019



Breaking Changes



Add an optional condition property to BlocListener which takes the previousState and currentState as arguments and must return a bool which determines whether or not call listener


import 'package:equatable/equatable.dart';

enum SessionState { notInitialized, signedIn, notSignedIn }

class AuthState extends Equatable {
  final bool isLoading;
  final SessionState sessionState;

  AuthState(this.isLoading, this.sessionState)
      : super([isLoading, sessionState]);
  Stream<AuthState> mapEventToState(AuthEvent event) async*{
    if (event == AuthEvent.SignOut) {
      yield currentState.copyWith(true);
      await _signOut();
      yield currentState.copyWith(false, SessionState.notSignedIn);
  Widget build(BuildContext context) {
    return BlocListener(
      bloc: authBloc,
      condition: (AuthState previous, AuthState current) =>
          previous.sessionState != current.sessionState,
      listener: (BuildContext context, AuthState state) {
        if (state.sessionState == SessionState.signedIn) {
          Navigator.pushNamed(context, HomeScreen.routeName);
      child: MyChild(),

In the above example, the listener push a widget only when sessionState change to signedIn.

Without condition, this widget push multiple times HomeScreen when isLoading change and sessionState is signedIn , for example when try to sign out, the state change because isLoading change but while sessionState is signedIn, the listener push HomeScreen again.

This is only a example, you can imagine other cases with other bloc, when you need to show a dialog, snakcbar, etc.


  • Tests
  • Documentation
  • Examples

Impact to Remaining Code Base


@GiancarloCode GiancarloCode requested a review from felangel as a code owner Jul 14, 2019


This comment has been minimized.

Copy link

commented Jul 14, 2019

Codecov Report

Merging #406 into master will not change coverage.
The diff coverage is 100%.

Impacted file tree graph

@@          Coverage Diff          @@
##           master   #406   +/-   ##
  Coverage     100%   100%           
  Files          11     11           
  Lines         174    179    +5     
+ Hits          174    179    +5
Impacted Files Coverage Δ
packages/flutter_bloc/lib/src/bloc_listener.dart 100% <100%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a6fe3e0...a484392. Read the comment docs.

Copy link

left a comment

Thanks for the contribution! LGTM 👍

@felangel felangel merged commit c5c82dc into felangel:master Jul 15, 2019

3 checks passed

codecov/patch 100% of diff hit (target 100%)
codecov/project 100% (+0%) compared to a6fe3e0
continuous-integration/travis-ci/pr The Travis CI build passed

@felangel felangel added this to Done in bloc Jul 15, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.