I have been using bloc pattern to create a login feature in my flutter application, and i have used the (flutter_bloc) plugin>>>> everything work good, but my question is should i create a separate bloc for (logout) or not, in my case i just add the events and state for logout with (auth bloc):
event: import 'package:equatable/equatable.dart';
abstract class AuthEvents extends Equatable{}
class StartedEvent extends AuthEvents {
@override
List<Object> get props => [];
}
class SignOutEvent extends AuthEvents{
@override
List<Object> get props => [];
}
class AuthLoggedInEvent extends AuthEvents {
final String token;
AuthLoggedInEvent({this.token});
@override
String toString() => 'LoggedIn { token: $token }';
@override
List<Object> get props => [token];
}
state:
class AuthStates extends Equatable{
@override
List<Object> get props => [];
}
class AuthInitialState extends AuthStates {
}
class AuthenticatedState extends AuthStates {
User user;
AuthenticatedState({this.user});
}
class UnauthenticatedState extends AuthStates {}
class LogOutSuccessState extends AuthStates{}
bloc:
@override
Stream<AuthStates> mapEventToState(AuthEvents event) async*{
try {
if(event is StartedEvent) {
var loggedIn = repo.isUserSignedIn();
if(loggedIn) {
User user = repo.getUser();
yield AuthenticatedState(user: user);
} else {
yield UnauthenticatedState();
}
} else if(event is SignOutEvent){
repo.signOut();
yield LogOutSuccessState();
yield UnauthenticatedState();
}
does it a good approach? or it is better to create a separate bloc for it? thanks
Does your "being logged out" state have it's own set of states and rules? Then you should create it's own bloc.
For example you could have one bloc that handles the sign up and registration when logged out. That seems to be a perfect example for it's own bloc.