我想将集团逻辑分离为不同的方法,但是由于某些原因,我不等待doLogIn()
。我不确定自己在做什么错。
@override
Stream<AuthState> mapEventToState(
AuthState currentState,
AuthEvent event,
) async* {
if (event is LogIn) {
await doLogIn();
}
}
doLogIn() async* {
try {
final userId = await _authRepository.signIn("xxxxxxxxxx@gmail.com", "xxxxxxxxxx");
yield AuthLogInSuccess();
final user = await _authRepository.getCurrentUser();
yield AuthUserAvailable(firebaseUser: user);
} catch (e) {
print(e);
yield AuthError();
}
}
答案 0 :(得分:1)
以下是如何分隔集团逻辑的示例:
清除集团:
class LoginBloc extends Bloc<LoginEvent, LoginState> {
@override
LoginState get initialState => new Loading();
@override
Stream<LoginState> mapEventToState(
LoginState currentState,
LoginEvent event,
) async* {
final newState = await event.getNewState(currentState);
yield newState;
}
}
状态:
abstract class LoginState { }
class Loading extends LoginState { }
class LoginFailed extends LoginState {
final String message;
LoginFailed(this.message);
}
class LoginSuccessfulComplete extends LoginState {
final User user;
LoginSuccessfulComplete(this.user);
}
我建议将转换逻辑应用于事件:
abstract class LoginEvent {
Future<LoginState> getNewState(LoginState state);
}
class AuthLoading extends LoginEvent {
@override
Future<LoginState> getNewState(LoginState state) async{
final _authService = new AuthService();
final UserIndentity identity = await _authService.onLoad();
if (identity.isEmpty) {
return PhoneEnter();
}
final user = await _authService.login(identity);
if (user == null){
return LoginFailed('user not found');
}
return LoginSuccessfulComplete(user);
}
}