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

feat: added addIf in bloc.dart in order to to allows conditional even… #4261

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/bloc/lib/src/bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,27 @@ abstract class Bloc<Event, State> extends BlocBase<State>
}
}

/// Adds an [event] to the [Bloc] if the given [condition] is true.
///
/// This method allows conditional event addition, which can be useful
/// for scenarios where events should only be added based on certain
/// conditions.
///
/// ```dart
/// bloc.addIf(() => MyEvent(), someCondition);
/// ```
///
/// * [eventBuilder] - A function that returns the event to be added.
/// * [condition] - A boolean that determines whether the event should be added.
void addIf<T>(
T Function() eventBuilder,
bool condition,
) {
if (condition) {
add(eventBuilder());
}
}

/// Called whenever an [event] is [add]ed to the [Bloc].
/// A great spot to add logging/analytics at the individual [Bloc] level.
///
Expand Down