/failure_or_value

This is a simplified version of Either type defined in dartz package and specifically used for returning a failure or value.

Primary LanguageDartApache License 2.0Apache-2.0

This is a simplified version of Either type defined in dartz package and specifically used for returning a failure or value.

Either<Failure, int> getFailure() {
  return failure(Failure());
}

Either<Failure, int> getNumber() {
  return success(10);
}

void test() async {
  final failureOrSuccess = getFailure();
  assert(failureOrSuccess.isFailure() == true);
  
  failureOrSuccess.fold(
      (failure) => null, // handle failure,
      (value) => null, // do something with value,
  );

  // This time returns a future
  final state = await failureOrSuccess.fold(
        (failure) async {
          await service.updateSomethingInServer();
          return State.failure(failure);
        },
        (value) async => State.success(value),
  );
}

// Example for creating Failure objects in catch bloc and sending "Exception" and "Stack Trace".
Future<Either<Failure, User>> getUser(String id) async {
  try {
    final user = await _userService.fetchUser(id);
    return success(user);
  } on PlatformException catch(e, st) {
    return failure(Failure.platform(e, st));
  } on Exception catch(e, st) {
    return failure(Failure.any(e, st));
  }
}

void useGetUserFunction() async {
  final failureOrSuccess = await getUser("12345");
  failureOrSuccess.fold(
      (failure) {
        print(failure.e);
        print(failure.st);
      },
      (value) => print(value),
  );
}