Cubit vs Bloc ⚔️

Syed Abdul Basit
5 min readOct 13, 2024

Choosing the Right Tool for Efficient State Management in Your Flutter App 🛠️

Two powerful approaches for managing state in Flutter are Cubit and Bloc from the flutter_bloc package. In this post, we'll break down the concepts of Cubit and Bloc, showing how to create and manage states, events, and event transformations.

Cubit (Counter Example)

import 'package:equatable/equatable.dart';

class CounterState extends Equatable {
final int counter;

const CounterState(this.counter);

factory CounterState.initial() => const CounterState(0);

CounterState copyWith({int? counter}) {
return CounterState(counter ?? this.counter);
}

@override
List<Object> get props => [counter];

@override
String toString() => 'CounterState(counter: $counter)';
}
  • We use Equatable to handle equality checks.
  • The copyWith method allows updating the state without modifying the entire object.
  • toString provides a readable representation of the state.

--

--

No responses yet