首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在事件处理程序正常完成后调用集迁移后发出的问题。

在事件处理程序正常完成后调用集迁移后发出的问题。
EN

Stack Overflow用户
提问于 2022-01-02 03:51:40
回答 1查看 232关注 0票数 0

在迁移到最新版本的bloc之后,我出现了一个错误。以下是迁移前的代码:

代码语言:javascript
运行
复制
  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();
    }
  }

下面是迁移后对代码所做的工作。虽然我不确定用对其进行编码,但仍然是一件好事。

代码语言:javascript
运行
复制
  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,,则会出现语法错误。请帮帮忙。谢谢!

代码语言:javascript
运行
复制
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):   });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-03 10:22:06

请按以下方式调整:

代码语言:javascript
运行
复制
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);
  }

同时调整你的功能:

代码语言:javascript
运行
复制
Future<void> mapSignInWithGoogleToState(
    SignInWithGoogle event,
    Emitter<SignInState> emit,
  ) async {
...
}

Future<void> mapUpdateLastLoginEventToState(
    UpdateLastLoginEvent event,
    Emitter<SignInState> emit,
  ) async {
...
}

使用try/catch方法仍然可以!

如果成功了请告诉我。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70553505

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档