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).


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();
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;
List<Object> get props => [currentUserData];
class AuthenticationNotAuthenticated extends AuthenticationState {}
class AuthenticationFailure extends AuthenticationState {
final String message;
AuthenticationFailure({@required this.message});
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();
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.password});
List<Object> get props => [email, password];
class UserLogin extends AuthenticationEvent {
final String email;
final String password;
UserLogin({@required, this.password});
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 =
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 {
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;