Skip to content

Instantly share code, notes, and snippets.

@felangel
Last active June 28, 2019 23:39
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/cfee4decff5548f58ee6740fed9bf43e to your computer and use it in GitHub Desktop.
Save felangel/cfee4decff5548f58ee6740fed9bf43e to your computer and use it in GitHub Desktop.
[flutter_login] Authentication 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';
class AuthenticationBloc
extends Bloc<AuthenticationEvent, AuthenticationState> {
final UserRepository userRepository;
AuthenticationBloc({@required this.userRepository})
: assert(userRepository != null);
@override
AuthenticationState get initialState => AuthenticationUninitialized();
@override
Stream<AuthenticationState> mapEventToState(
AuthenticationState currentState,
AuthenticationEvent event,
) async* {
if (event is AppStarted) {
final bool hasToken = await userRepository.hasToken();
if (hasToken) {
yield AuthenticationAuthenticated();
} else {
yield AuthenticationUnauthenticated();
}
}
if (event is LoggedIn) {
yield AuthenticationLoading();
await userRepository.persistToken(event.token);
yield AuthenticationAuthenticated();
}
if (event is LoggedOut) {
yield AuthenticationLoading();
await userRepository.deleteToken();
yield AuthenticationUnauthenticated();
}
}
}
@mirkancal
Copy link

I think with your current BLoC package, mapEventToState function takes one parameter. Warning goes away when I delete AuthenticationState currentState parameter from the function.

'AuthenticationBloc.mapEventToState' ('(AuthenticationState, AuthenticationEvent) → Stream<AuthenticationState>') isn't a valid override of 'Bloc.mapEventToState' ('(AuthenticationEvent) → Stream<AuthenticationState>').dart(invalid_override)

@felangel
Copy link
Author

@mirkancal this example is using an older version of bloc. You can check out the most up-to-date version here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment