0

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

Osama Mohammed
  • 2,433
  • 13
  • 29
  • 61

1 Answers1

1

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.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • thanks for your response, please check my code above, you can see i just need one state and one event for logout .... because i have Auth bloc which i add signout events and state inside it, so auth bloc using for check if user Authenticated (like signed in) or unauthenticated (like singnout) – Osama Mohammed Oct 12 '20 at 08:47
  • 1
    I'm not sure I understand the question. If "logged out" is a single state with no logic, then no, you don't need another bloc, what would that do? – nvoigt Oct 12 '20 at 13:45