在迁移到最新版本的bloc之后,我出现了一个错误。以下是迁移前的代码:
SignInBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignInInitialState());
SignInState get initialState => SignInInitialState();
@override
Stream<SignInState> mapEventToState(
SignInEvent event,
) async* {
if (event is SignInWithGoogle) {
yield* mapSignInWithGoogleToState();
}
if (event is UpdateLastLoginEvent) {
yield* mapUpdateLastLoginEventToState(event.uid, event.deviceID);
}
}
Stream<SignInState> mapSignInWithGoogleToState() async* {
yield SignInWithGoogleInProgressState();
try {
String res = await authenticationRepository.signInWithGoogle();
yield SignInWithGoogleCompletedState(res);
} catch (e) {
print(e);
yield SignInWithGoogleFailedState();
}
}
Stream<SignInState> mapUpdateLastLoginEventToState(
String uid, String deviceID) async* {
yield UpdateLastLoginInProgressState();
try {
String? res = await userDataRepository.lastLogin(uid, deviceID);
if (res != null) {
yield UpdateLastLoginCompletedState(res);
} else {
yield UpdateLastLoginFailedState();
}
} catch (e) {
print(e);
yield UpdateLastLoginFailedState();
}
}
下面是迁移后对代码所做的工作。虽然我不确定用对其进行编码,但仍然是一件好事。
SignInBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignInInitialState()) {
on<SignInEvent>((event, emit) async {
if (event is SignInWithGoogle) {
mapSignInWithGoogleToState(emit);
}
if (event is UpdateLastLoginEvent) {
mapUpdateLastLoginEventToState(emit, event.uid, event.deviceID);
}
});
}
Future<void> mapSignInWithGoogleToState(
Emitter<SignInState> emit,
) async {
emit(SignInWithGoogleInProgressState());
try {
String res = await authenticationRepository.signInWithGoogle();
emit(SignInWithGoogleCompletedState(res));
} catch (e) {
print(e);
emit(SignInWithGoogleFailedState());
}
}
Future<void> mapUpdateLastLoginEventToState(
Emitter<SignInState> emit,
String uid,
String deviceID,
) async {
emit(UpdateLastLoginInProgressState());
try {
String? res = await userDataRepository.lastLogin(uid, deviceID);
if (res != null) {
emit(UpdateLastLoginCompletedState(res));
} else {
emit(UpdateLastLoginFailedState());
}
} catch (e) {
print(e);
emit(UpdateLastLoginFailedState());
}
}
下面是我在日志中看到的内容,但是如果我试图实现future.whenComplete,,则会出现语法错误。请帮帮忙。谢谢!
I/flutter (18083): emit was called after an event handler completed normally.
I/flutter (18083): This is usually due to an unawaited future in an event handler.
I/flutter (18083): Please make sure to await all asynchronous operations with event handlers
I/flutter (18083): and use emit.isDone after asynchronous operations before calling emit() to
I/flutter (18083): ensure the event handler has not completed.
I/flutter (18083):
I/flutter (18083): **BAD**
I/flutter (18083): on<Event>((event, emit) {
I/flutter (18083): future.whenComplete(() => emit(...));
I/flutter (18083): });
I/flutter (18083):
I/flutter (18083): **GOOD**
I/flutter (18083): on<Event>((event, emit) async {
I/flutter (18083): await future.whenComplete(() => emit(...));
I/flutter (18083): });
发布于 2022-01-03 10:22:06
请按以下方式调整:
SignInBloc(
{required this.authenticationRepository,
required this.userDataRepository})
: super(SignInInitialState()) {
// change this
/*on<SignInEvent>((event, emit) {
if (event is SignInWithGoogle) {
mapSignInWithGoogleToState(emit);
}
if (event is UpdateLastLoginEvent) {
mapUpdateLastLoginEventToState(emit, event.uid, event.deviceID);
}
});*/
// to this
on<SignInWithGoogle>(mapSignInWithGoogleToState);
on<UpdateLastLoginEvent>(mapUpdateLastLoginEventToState);
}
同时调整你的功能:
Future<void> mapSignInWithGoogleToState(
SignInWithGoogle event,
Emitter<SignInState> emit,
) async {
...
}
Future<void> mapUpdateLastLoginEventToState(
UpdateLastLoginEvent event,
Emitter<SignInState> emit,
) async {
...
}
使用try/catch方法仍然可以!
如果成功了请告诉我。
https://stackoverflow.com/questions/70553505
复制相似问题