Every developer in this universe write code, but it sounds really nice when someone says this developer writes clean code. As flutter framework got only Dart language to write UI code and also business logic, so everything become messy when both UI code and business logic mix up.
Question: How do we can stop our code from being messy?
Note: We’re already using Clean Architecture for separation of concern but this situation is different.
Answer: There comes our Bloc library but we will use its lighter version Cubit which removes bunch of boilerplate code, Cubit not required any kind of package to add but it comes with the package flutter_bloc of version 6.0.0 or above.
Don’t know what is Bloc?
Bloc is well-known and established state management library for flutter, which promotes immutability and has one of the best ecosystems of supporting packages and documentation built around it.
Don’t know what is Cubit?
Cubit is lightweight state management solution. It is subset of the bloc package that does not rely on events and instead uses methods to emit new states. Every Cubit requires an initial state which will be the state of the Cubit before emit has been called.
Bloc
If you’ve used the bloc in past you may be familiar with this diagram.
This Bloc architecture self-explained. Let’s understand it, In Bloc we’ve Events and States and Bloc itself.
Event: Events tell bloc to do something, Triggered button click, method call etc.
State: States represent the info to be processed by any widget, Initial, Loading, Loaded and Failure State etc.
Bloc: All the business logic take place inside the Bloc, It accept events, perform the logic and output states.
In General: We’ve events (click, trigger, call) and states (initial, loading, loaded etc.) Bloc accept events perform the logic and output a specific state.
Cubit
If you’ve used the cubit in past you may be familiar with this diagram.
This Cubit architecture is also self-explained. Let’s understand it, in cubit we’ve States and Cubit itself.
State: States represent the info to be processed by any widget, Initial, Loading, Loaded and Failure State etc.
Cubit: Cubit exposes direct functions and outputs appropriate states.
In General: We’ve States (Initial, Loading, Loaded and Failure), Cubit directly take functions and emits an appropriate state.
Create Directories:
Go to > presentation > cubit and create 3 directories, user, credential and auth. Now create cubit class inside all of them with specific names.
If you’re using Android Studio install a plugin of Bloc.
Go to File > Settings > Plugins > Market Place install Bloc.
If you’re using VS Code install and extension of Bloc.
After creating Cubit Classes your directories will look something like this:
Auth Cubit:
auth_state.dart
part of 'auth_cubit.dart';
abstract class AuthState extends Equatable {
const AuthState();
}
class AuthInitial extends AuthState {
@override
List
part of 'credential_cubit.dart';
abstract class CredentialState extends Equatable {
const CredentialState();
}
class CredentialInitial extends CredentialState {
@override
List get props => [];
}
class CredentialLoading extends CredentialState {
@override
List get props => [];
}
class CredentialSuccess extends CredentialState {
@override
List get props => [];
}
class CredentialFailure extends CredentialState {
@override
List get props => [];
}
part of 'user_cubit.dart';
abstract class UserState extends Equatable {
const UserState();
}
class UserInitial extends UserState {
@override
List get props => [];
}
class UserLoading extends UserState {
@override
List get props => [];
}
class UserLoaded extends UserState {
final List users;
UserLoaded({required this.users});
@override
List get props => [users];
}
class UserFailure extends UserState {
@override
List get props => [];
}
We’ve successfully created Cubits for our User and in the next article we will explore dependency injection and also will call these methods. In order to not miss next video be sure to subscribe to the channel and hit the bell icon to make sure you get notified whenever the next video is uploaded.