bloc_simple 1.0.0 copy "bloc_simple: ^1.0.0" to clipboard
bloc_simple: ^1.0.0 copied to clipboard

A package that makes life more easier

text

BlocSimple #

Pub Version License: MIT

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 operations
  • SimpleState.loading(): Loading state during operations
  • SimpleState.success(data): Success state with data
  • SimpleState.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.

2
likes
0
points
95
downloads

Publisher

unverified uploader

Weekly Downloads

A package that makes life more easier

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

bloc, bloc_test, cupertino_icons, equatable, flutter, flutter_bloc

More

Packages that depend on bloc_simple