I have a doubt about an asynchronous situation, I have the following function:
@override
Future<Consulta> checkPayment() async {
String url = 'http......'
final response = await HttpClientAdapter.get(url: url);
if (response.statusCode == 200 || response.statusCode == 201) {
final respuestaConsulta =
Consulta.fromJson(json.decode(response.body));
// (1) PRIMERA LLAMADA EL RESULTADO ES PENDING
// (2) SEGUNDA LLAMADA EL RESULTADO ES DECLINED
print(respuestaConsulta.data.status);
if (respuestaConsulta.data.status == 'PENDING') {
await Future.delayed(const Duration(seconds: 1), checkPayment);
}
// (3) SE EJECUTA AL FINALIZAR PERO EL RESULTADO SIEMPRE ES PENDING,
// DEBERIA RETORNAR EL RESULTADO DE LA TRANSACCION, EN ESTE CASO DECLINED
return respuestaConsulta;
} else {
throw ArgumentError(response.body);
}
}
The general idea is that the function long polls the API until the status of the transaction is something other than "PENDING"; To do this, if the result is not the desired one, the function is executed again with the Future so that the API call is repeated.
Apparently everything works fine, but I have only one drawback:
In the break (1) => The result is PENDING as it is the first call
In the break (2) => The result is DECLINED (After re-executing with the Future)
At break (3) => The result is still PENDING (even after making the previous calls again)
Therefore, when calling the function, the result will always be PENDING.
final check = await checkPayment(); // AQUI SIEMPRE RETORNA PENDING
The return should not return the result of the last execution of this method?
final respuestaConsulta = Consulta.fromJson(json.decode(response.body));