bloc_simple 1.0.0
bloc_simple: ^1.0.0 copied to clipboard
A package that makes life more easier
text
BlocSimple #
A simplified approach to using the BLoC pattern in Flutter, reducing boilerplate and providing utilities for common patterns.
Features #
- ๐ Simplified State Management: Pre-defined state types with built-in loading/error handling
- ๐ Reduced Boilerplate: Write less code while maintaining the BLoC pattern benefits
- ๐ Repository Integration: Easy connection between BLoCs/Cubits and data sources
- ๐งช Testing Support: Helper methods for quick and easy testing
- โก Async Operation Handling: Automatic state transitions during async operations
Installation #
Add bloc_simple
to your pubspec.yaml
:
dependencies: bloc_simple: ^0.1.0
text
Usage #
Simple Counter Example #
// 1. Create a Cubit class CounterCubit extends SimpleCubit
void increment() { final currentValue = state.data ?? 0; setSuccess(currentValue + 1); }
void decrement() { final currentValue = state.data ?? 0; setSuccess(currentValue - 1); } }
// 2. Use it in your UI class CounterView extends StatelessWidget { @override Widget build(BuildContext context) { return BlocBuilder<CounterCubit, SimpleState
text if (state.isError) { return Text('Error: ${state.message}'); }
return Text(
'Count: ${state.data ?? 0}',
style: TextStyle(fontSize: 24),
);
}, ); } }
text
API Example with Repository #
// 1. Define your repository class UserRepository { Future<List
// 2. Create a Cubit with repository integration class UserCubit extends RepositoryCubit<List
Future
// 3. Use in your UI with automatic state handling class UserListView extends StatelessWidget { @override Widget build(BuildContext context) { return BlocBuilder<UserCubit, SimpleState<List
text if (state.isLoading) { return Center(child: CircularProgressIndicator()); }
if (state.isError) {
return Center(child: Text('Error: ${state.message}'));
}
final users = state.data!;
return ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) => ListTile(
title: Text(users[index].name),
subtitle: Text(users[index].email),
),
);
}, ); } }
text
Using SimpleBloc with Events #
// 1. Define events abstract class CounterEvent {} class CounterIncremented extends CounterEvent {} class CounterDecremented extends CounterEvent {}
// 2. Create a Bloc class CounterBloc extends SimpleBloc<CounterEvent, int> { CounterBloc() : super();
@override void registerEventHandlers() { on
text // Initialize with 0 setSuccess(0); }
void _onIncremented(CounterIncremented event, Emitter<SimpleState
void _onDecremented(CounterDecremented event, Emitter<SimpleState
text
State Types #
SimpleState<T>
provides a standardized approach to handling common states:
SimpleState.initial()
: Initial state before any operationsSimpleState.loading()
: Loading state during operationsSimpleState.success(data)
: Success state with dataSimpleState.error(exception)
: Error state with exception details
Additional Features #
- Automatic Error Handling: The
executeAsync
method wraps operations in try-catch blocks - Message Support: Add custom messages to success and error states
- Repository Connector: Mix-in for easy repository integration
- Testing Utilities: Helper methods for testing BLoCs and Cubits
Example Project #
Check out the example directory for a complete sample application.
License #
MIT License - see the LICENSE file for details.