So I have a Flutter app with Bloc for state mangement. I want to keep a list of users in Bloc so I can access it from different places in the app. But as it is now I pass this list of users to every state. Can I somehow avoid this boilerplate code?
Here is an example of my Bloc State:
@immutable
abstract class UsersState {
final List<User> users;
final User? user;
const UsersState({required this.users, required this.user});
}
class UsersInitial extends UsersState {
UsersInitial() : super(users: [], user: null);
}
class UsersLoadedState extends UsersState {
const UsersLoadedState({required List<User> users, required User? user})
: super(users: users, user: user);
}
class UsersPendingState extends UsersState {
const UsersPendingState({List<User> users = const [], User? user})
: super(users: users, user: user);
}
class UsersErrorState extends UsersState {
final String message;
const UsersErrorState(
{required List<User> users, required User? user, required this.message})
: super(users: users, user: user);
}
class FriendAddedState extends UsersState {
const FriendAddedState({required List<User> users, required User? user})
: super(users: users, user: user);
}
In this example I don’t want to have to pass list of users to FriendAddedState.
And here is how it looks in Bloc:
else if (event is AddFriend) {
emit(FriendAddPending(users: users, user: user));
final result = await usecases.addFriend(event.friendId);
result.fold((error) {
emit(UsersErrorState(
users: users, user: user, message: error.message));
}, (success) {
user = success;
emit(FriendAddedState(users: users, user: user));
});
}
4
Answers
You could use:
context.read<YourBlocClass>();
or
BlocProvider.of<YourBlocClass>(context);
Hope this helps.
You could try to use a two different blocs, one for the state, one for the list of users.
An alternative can be to have just one state class with an enum for all the possible states.
This way you don’t reinitialize the class every state change. Something like:
You don’t have to define
users
anduser
inUserState
if you don’t need them in all the bloc states. You could just define them inUsersLoadedState
FriendAddedState
like this:[Recommended] if you want to access user in every state of bloc, you can use a single class for your bloc state and an enum to define its state:
then in your bloc class, anywhere you wanted to emit new state, use
state.copyWith()
!You can make users optional so that they are not required in every state.
Without null safety
With null safety