Dart - Why do I have to await this either.fold call

982

Okay I think I figured it out, because I set (user) async {} it made the fold a Future, but the other half wasn't async (failure) => left(failure) so i assume i was getting the error because of that.

Once i set the failure to be async the error went away. (failure) async => left(failure)

Bit of a silly question but i'll leave it up incase anyone gets the same error.

Share:
982
Donk3y
Author by

Donk3y

Updated on December 17, 2022

Comments

  • Donk3y
    Donk3y over 1 year
        Future<Either<Failure, User>> call(SignUpParams params) async {
    
        Either<Failure, User> failureOrUser;
    
        // Creates a User
        failureOrUser = await repository.signUpUser(
            params.email, params.password, params.type);
    
        // Creates a [Learner / Instructor] if User returned
        await failureOrUser.fold(
                (failure) => left(failure),
                (user) async {
                  final remoteServerFailureOrSuccess =
                    await createLearnerOrInstructor(CreateLOIParam(user: user));
    
                  // check if [Learner / Instructor] creation has failed
                  remoteServerFailureOrSuccess.fold(
                          (failure) => failureOrUser = left(failure),
                          (success) => null
                  );
    
                }
        );
    
        return failureOrUser;
    } 
    

    I cant figure out why I need to place an await in front of failureOrUser.fold(); method.

    If I dont then the (user) async {} method doesnt await

    final remoteServerFailureOrSuccess = await createLearnerOrInstructor(CreateLOIParam(user: user));
    

    and return failureOrUser; is called before

    remoteServerFailureOrSuccess.fold(
      (failure) => failureOrUser = left(failure),
      (success) => null
    );
    

    is called.

    Im getting an 'await is applied before 'Object', which is not a Future' error tip but the code only waits till the whole method is finished before returning, done this way.

    Ive tried putting

    await remoteServerFailureOrSuccess.fold(
     (failure) => failureOrUser = left(failure),
     (success) => null
    );
    

    but this still doesn't work.

    So to me it seems like

    final remoteServerFailureOrSuccess = await createLearnerOrInstructor(CreateLOIParam(user: user));
    

    isn't actually awaiting.