Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active January 21, 2019 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felangel/6231045fc3bd593ac7ab943b83505a12 to your computer and use it in GitHub Desktop.
Save felangel/6231045fc3bd593ac7ab943b83505a12 to your computer and use it in GitHub Desktop.
[flutter_login] Login Bloc
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:bloc/bloc.dart';
import 'package:user_repository/user_repository.dart';
import 'package:flutter_login/authentication/authentication.dart';
import 'package:flutter_login/login/login.dart';
class LoginBloc extends Bloc<LoginEvent, LoginState> {
final UserRepository userRepository;
final AuthenticationBloc authenticationBloc;
LoginBloc({
@required this.userRepository,
@required this.authenticationBloc,
}) : assert(userRepository != null),
assert(authenticationBloc != null);
@override
LoginState get initialState => LoginInitial();
@override
Stream<LoginState> mapEventToState(
LoginState currentState,
LoginEvent event,
) async* {
if (event is LoginButtonPressed) {
yield LoginLoading();
try {
final token = await userRepository.authenticate(
username: event.username,
password: event.password,
);
authenticationBloc.dispatch(LoggedIn(token: token));
yield LoginInitial();
} catch (error) {
yield LoginFailure(error: error.toString());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment