Last active
June 28, 2019 23:39
-
-
Save felangel/cfee4decff5548f58ee6740fed9bf43e to your computer and use it in GitHub Desktop.
[flutter_login] Authentication Bloc
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
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 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
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)