Skip to content

Instantly share code, notes, and snippets.

@Sumit-Ghosh
Last active February 20, 2022 06:47
Show Gist options
  • Save Sumit-Ghosh/e5586a25bda7615f574c9a2497984429 to your computer and use it in GitHub Desktop.
Save Sumit-Ghosh/e5586a25bda7615f574c9a2497984429 to your computer and use it in GitHub Desktop.
import 'package:bloc/bloc.dart';
part 'login_event.dart';
part 'login_state.dart';
/*
We extend the the Bloc class and also mention the
type for event and state.
*/
class LoginBloc extends Bloc<LoginEvent, LoginState> {
/*
* 1. This is a constructor method of
* the Bloc, incase of making API you will inject repository.
* 2. We are passing the first event to the super class,
* so, this is the first event that get emitted,
* we can use it in the UI, to run some setup code.
*/
LoginBloc() : super(LoginInitial()) {
/*
* 1. With this method we register event handle with event type,
* In this case we are using Login Event,
* as this Bloc, operates on LoginEvent and LoginState
* 2. This method takes two parameters Handler and transformer,
* Transformer for is a optional method, we are not using it here.
*/
// Note: Every event should have only one handler
on<LoginEvent>(_loginEventHandler);
/*
Will be used Later
on<LoginButtonTappedEvent>(_loginButtonTapped);
on<ShowSnackBarButtonTappedEvent>(_showSnackBarTapped);
*/
}
/*
* 1. This method returns Future void, which indicates,
* it is capable of performing async operations.
* 2. It takes in two required parameter,
* Event and a emitter
*/
Future<void> _loginEventHandler(LoginEvent e, Emitter emit) async {
// Perform some task run some business login
/*
* Emitter class have few other methods,
* but we are using the most simple one
* as we just need to emit an state.
*/
emit(LoginInitial());
}
/*
Will be used Later
Future<void> _loginButtonTapped(LoginButtonTappedEvent e, Emitter emit) async {
emit(UpdateTextState(text: "Text is sent from the Bloc"));
}
Future<void> _showSnackBarTapped(ShowSnackBarButtonTappedEvent e, Emitter emit) async {
emit(ShowSnackbarState());
}*/
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment