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

Conversation

Danilospano00
Copy link

Status

READY

Breaking Changes

NO

Description

This pull request introduces a new method to the Bloc class in the bloc.dart file, allowing for conditional event addition.

Enhancements to Bloc class:

  • packages/bloc/lib/src/bloc.dart: Added the addIf method to the Bloc class, which allows adding an event based on a given condition. This is useful for scenarios where events should only be added under specific conditions.

This functionality simplifies the process of adding events conditionally, reducing repetitive if statements outside of the Bloc and promoting cleaner, more maintainable code.

This is an example using addIf method:

BlocProvider<MyBloc>(
              create: (context) => MyBloc(
                myRepository: context.read<MyRepository>(),
              )..addIf(
                  condition: condition == true,
                  eventBuilder: () => AddEvent(),
                ),
              child: HomePage(),
            );

Example without addIf method:

if(condition == true) {
              return BlocProvider<MyBloc>(
              create: (context) => MyBloc(
                myRepository: context.read<MyRepository>(),
              )..add(AddEvent()),
              child: HomePage(),
            );
} else {
 return BlocProvider<MyBloc>(
              create: (context) => MyBloc(
                myRepository: context.read<MyRepository>(),
              ),
              child: HomePage(),
            );

}

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

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

Successfully merging this pull request may close these issues.

1 participant