Flutter bloc 8 在 Either / fold return with async 中出现一些错误

Flutter bloc 8 some error in Either / fold return with async

我正在将我的项目迁移到新版本的 Flutter,我使用的是 BloC 包。如您所知,Bloc 已经更新并且更改了大部分代码,所以现在我正在调整我的代码,但我有一些错误。

The argument type 'Future Function(bool)' can't be assigned to the parameter type 'UserState Function(bool)

The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.

之前:

  Stream<UserState> _validate(ValidateEvent event) async* {

    final successOrFailure = await services.validate();
    
    yield* successOrFailure.fold(
      (sucess) async* {
        final session = await services.getSession();
        yield* session.fold(
          (session) async* {
            yield UserSuccess(session);

            yield UserLogged(session);
          },
          (failure) async* {
            yield UserError(failure.message);
          },
        );
      },
      (failure) async* {
        yield UserError(failure.message);
      },
    );
  }

之后:

UserBloc({this.services}) : super(UserInitial()) {
    on<ValidateEvent>(_onValidate);
  }

void _onValidate(
    ValidateEvent event,
    Emitter<UserState> emit,
  ) async {

    final successOrFailure = await services.validate();
    
    emit(
      successOrFailure.fold(
        (success) async {
          final session = await services.getSession();
          emit(
            session.fold(
              (session) { // ! The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
                emit(UserSuccess(session));

                emit(UserLogged(session));
              },
              (failure) { // ! The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
                emit(UserError(failure.message));
              },
            ),
          );
        }, // ! The argument type 'Future<Null> Function(bool)' can't be assigned to the parameter type 'UserState Function(bool)
        (failure) { // ! The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
          emit(UserError(failure.message));
        },
      ),
    );
  }

我需要在内部使用异步,因为我再次调用该服务,关于主体,在我看来我必须以某种方式 return 它,但我内部有一些逻辑但我没有知道怎么做了。

您唯一可以 emit 的是您的状态类型的实例,在本例中为 UserState。您应该 await 它而不是尝试发射 Future。

void _onValidate(
    ValidateEvent event,
    Emitter<UserState> emit,
  ) async {

    final successOrFailure = await services.validate();
    
    await successOrFailure.fold( // This will return FutureOr<Null>
      (success) async {
        final session = await services.getSession();
        session.fold( // This isn't asynchronous, so no need to await here
          (session) {
            emit(UserSuccess(session));

            emit(UserLogged(session));
          },
          (failure) {
            emit(UserError(failure.message));
          },
        );
      },
      (failure) {
        emit(UserError(failure.message));
      },
    );
  }