Last active
October 14, 2018 08:30
-
-
Save felangel/25c6fcf07c59f87de0a733fbeec7af47 to your computer and use it in GitHub Desktop.
[bloc_package] LoginBloc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LoginBloc extends Bloc<LoginEvent, LoginState> { | |
LoginState get initialState => LoginState.initial(); | |
void onLoginButtonPressed({String username, String password}) { | |
dispatch( | |
LoginButtonPressed( | |
username: username, | |
password: password, | |
), | |
); | |
} | |
@override | |
Stream<LoginState> mapEventToState(LoginState state, LoginEvent event) async* { | |
if (event is LoginButtonPressed) { | |
yield LoginState.loading(); | |
try { | |
final token = await _authenticate(event.username, event.password); | |
yield LoginState.success(token); | |
} catch (error) { | |
yield LoginState.failure(error.toString()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment