Created
February 27, 2019 04:02
-
-
Save felangel/6614b9ce0a536ef462138a0ba698053a to your computer and use it in GitHub Desktop.
Bloc Exception Handling
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:bloc/bloc.dart'; | |
import 'package:equatable/equatable.dart'; | |
abstract class AuthenticationEvent extends Equatable { | |
AuthenticationEvent([List props = const []]) : super(props); | |
} | |
class LoginEvent extends AuthenticationEvent { | |
final String loginRequest; | |
LoginEvent(this.loginRequest) : super([loginRequest]); | |
@override | |
String toString() { | |
return 'LoginEvent{loginRequest: $loginRequest}'; | |
} | |
} | |
abstract class AuthenticationState extends Equatable { | |
AuthenticationState([List props = const []]) : super(props); | |
} | |
class AuthenticationStateUnInitialized extends AuthenticationState { | |
@override | |
String toString() => 'AuthenticationStateUnInitialized'; | |
} | |
class AuthenticationStateLoading extends AuthenticationState { | |
@override | |
String toString() => 'AuthenticationStateLoading'; | |
} | |
class AuthenticationStateSuccess extends AuthenticationState { | |
String user; | |
AuthenticationStateSuccess(this.user) : super([user]); | |
@override | |
String toString() => 'AuthenticationStateSuccess{ user: $user }'; | |
} | |
class AuthenticationStateError extends AuthenticationState { | |
final String error; | |
AuthenticationStateError(this.error) : super([error]); | |
@override | |
String toString() => 'AuthenticationStateError { error: $error }'; | |
} | |
class AuthenticationBloc | |
extends Bloc<AuthenticationEvent, AuthenticationState> { | |
AuthenticationBloc(); | |
@override | |
AuthenticationState get initialState => AuthenticationStateUnInitialized(); | |
@override | |
Stream<AuthenticationState> mapEventToState( | |
AuthenticationState currentState, | |
AuthenticationEvent event, | |
) async* { | |
if (event is LoginEvent) { | |
yield AuthenticationStateLoading(); | |
try { | |
final result = await _login(event.loginRequest); | |
yield AuthenticationStateSuccess(result); | |
} catch (e) { | |
yield AuthenticationStateError("error processing login request"); | |
} | |
} else { | |
print("unknown"); | |
} | |
} | |
Future<String> _login(dynamic request) async { | |
throw Exception(); | |
} | |
@override | |
void onTransition( | |
Transition<AuthenticationEvent, AuthenticationState> transition, | |
) { | |
print(transition); | |
} | |
@override | |
void onError(Object error, StackTrace stacktrace) { | |
print('error in bloc $error $stacktrace'); | |
} | |
} | |
void main() async { | |
AuthenticationBloc authBloc = AuthenticationBloc(); | |
authBloc.state.listen((authState) { | |
print(authState); | |
}); | |
authBloc.dispatch(LoginEvent('blah')); | |
await Future.delayed(Duration(seconds: 3)); | |
authBloc.dispatch(LoginEvent('blah')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@astubenbord @felangel
I'm experimenting with this idea, I have a proof of concept implementation of this second stream in a Cubit, it works well but, again, is a very experimental and rough implementation.
Never had the time to do it in a proper way.
This is the implementation, again it's just a fast and dirty impl and tbh it's not something
I really worked on, just an experiment. Much of the stuff I just copy/pasted from the original classes to check if it was a good idea or not.
I'm sure it can be done in a much much better way.