Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active October 14, 2018 08:30
Show Gist options
  • Save felangel/25c6fcf07c59f87de0a733fbeec7af47 to your computer and use it in GitHub Desktop.
Save felangel/25c6fcf07c59f87de0a733fbeec7af47 to your computer and use it in GitHub Desktop.
[bloc_package] LoginBloc
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