1

I am using dartz with flutter. I want to use Either type with all API calls as suggested in this article

Below is the way I have wrapped API to Task

Future<Either<ApiException, SendOtpDto>> receiveOtp(
  String phoneNumber) async {
return Task<SendOtpDto>(() async {
  final String url = 'AuthenticationApi/sendOtp/$phoneNumber';
  final Map<String, dynamic> response = await apiWrapper.get(url);
  final SendOtpDto dto = SendOtpDto.fromJson(response);
  if (!dto.success) {
    throw ServerErrorException(dto.error); //Exception is thrown here
  }
  return dto;
}).attempt().mapLeftToFailure().run();
}

I expect attempt() should catch all throw as ApiException. Currently it gives error type 'Future<Either<ApiException, dynamic>>' is not a subtype of type 'FutureOr<Either<ApiException, SendOtpDto>>

Below is how I am using the Api

if (event is GetOtpButtonPressed) {
  yield LoginApiLoading();
  final Either<ApiException, SendOtpDto> result = await apiManager.authentication(context).receiveOtp(event.username);
  yield result.fold<LoginState>(
  (ApiException exception) { return LoginFailure(error: exception.toString()); }, 
  (SendOtpDto dto) {  return LoginOtpSent(dto: dto);  });
}
harishr
  • 17,807
  • 9
  • 78
  • 125

0 Answers0