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
merged 1 commit into from
Jul 15, 2019
Merged

Add optional condition to BlocListener #406

merged 1 commit into from
Jul 15, 2019

Conversation

GiancarloCode
Copy link
Contributor

Status

Development

Breaking Changes

NO

Description

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

Usage

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]);
}
...
@override
  Stream<AuthState> mapEventToState(AuthEvent event) async*{
    if (event == AuthEvent.SignOut) {
      yield currentState.copyWith(true);
      await _signOut();
      yield currentState.copyWith(false, SessionState.notSignedIn);
    }
  }
...
...
@override
  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.

Todos

  • Tests
  • Documentation
  • Examples

Impact to Remaining Code Base

None

@codecov
Copy link

codecov bot 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.

@felangel felangel added the enhancement New feature or request label Jul 15, 2019
Copy link
Owner

@felangel felangel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! LGTM 👍

@felangel felangel merged commit c5c82dc into felangel:master Jul 15, 2019
@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
Labels
enhancement New feature or request
Projects
bloc
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

2 participants