Add BLoC

Bloc will have modules for every API to fetch data and provide it to the UI. The module folder will have 4 files: events, states, bloc and public(which will export all the files).

/shared
/lib
/modules
/authentication
/bloc
/authentication-bloc
/authentication_bloc.dart
/authentication_state.dart
/authentication_event.dart
/authentication_bloc_public.dart
/bloc_controller.dart

Insert the following in the authentication_state.dart file:

import 'package:meta/meta.dart';
import 'package:equatable/equatable.dart';
import 'package:shared/modules/authentication/models/current_user_data.dart';
abstract class AuthenticationState extends Equatable {
const AuthenticationState();
@override
List<Object> get props => [];
}
class AppAutheticated extends AuthenticationState {}
class AuthenticationInitial extends AuthenticationState {}
class AuthenticationLoading extends AuthenticationState {}
class AuthenticationStart extends AuthenticationState {}
class UserLogoutState extends AuthenticationState {}
class SetUserData extends AuthenticationState {
final CurrentUserData currentUserData;
SetUserData({this.currentUserData});
@override
List<Object> get props => [currentUserData];
}
class AuthenticationNotAuthenticated extends AuthenticationState {}
class AuthenticationFailure extends AuthenticationState {
final String message;
AuthenticationFailure({@required this.message});
@override
List<Object> get props => [message];
}

Add the code shown below to the authentication_event.dart file:

import 'package:meta/meta.dart';
import 'package:equatable/equatable.dart';
abstract class AuthenticationEvent extends Equatable {
const AuthenticationEvent();
@override
List<Object> get props => [];
}
// Fired just after the app is launched
class AppLoadedup extends AuthenticationEvent {}
class UserLogOut extends AuthenticationEvent {}
class GetUserData extends AuthenticationEvent {}
class UserSignUp extends AuthenticationEvent {
final String email;
final String password;
UserSignUp({@required this.email, this.password});
@override
List<Object> get props => [email, password];
}
class UserLogin extends AuthenticationEvent {
final String email;
final String password;
UserLogin({@required this.email, this.password});
@override
List<Object> get props => [email, password];
}

Insert the following in the authentication_bloc.dart file:

class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
AuthenticationBloc() : super(AuthenticationInitial());
final AuthenticationRepository authenticationService =
AuthenticationRepository();
@override
Stream<AuthenticationState> mapEventToState(
AuthenticationEvent event) async* {
final SharedPreferences sharedPreferences = await prefs;
if (event is AppLoadedup) {
yield* _mapAppSignUpLoadedState(event);
}
if (event is UserSignUp) {
yield* _mapUserSignupToState(event);
}
if (event is UserLogin) {
yield* _mapUserLoginState(event);
}
if (event is UserLogOut) {
sharedPreferences.setString('authtoken', null);
sharedPreferences.setInt('userId', null);
yield UserLogoutState();
}
if (event is GetUserData) {
int currentUserId = sharedPreferences.getInt('userId');
final data = await authenticationService.getUserData(currentUserId ?? 4);
final currentUserData = CurrentUserData.fromJson(data);
yield SetUserData(currentUserData: currentUserData);
}
}

Insert the code shown below in the authentication_bloc_public.dart file:

export 'package:shared/modules/authentication/bloc/authentication/authentication_bloc.dart';
export 'package:shared/modules/authentication/bloc/authentication/authentication_event.dart';
export 'package:shared/modules/authentication/bloc/authentication/authentication_state.dart';

Add the code shown below to the bloc_controller.dart file:

import 'package:shared/main.dart';
class BlocController {
BlocController._();
static BlocController _instance = BlocController._();
factory BlocController() => _instance;
AuthenticationBloc authenticationBloc = AuthenticationBloc();
}

To use it in the UI, you can access the instance as shown below:

AuthenticationBloc authenticationBloc = BlocController().authenticationBloc;