0

I am developing an application with Flutter and I am using an HTTP packet for HTTP requests. I have this problem related to Wi-Fi connection. When a user clicks a button (such as a confirm button) that executes an HTTP POST request, there is a chance that the user will lose the Wi-Fi connection right then and there. Obviously, the application does not send the request and it still remains available for the button to be clicked again. Now, let's say the user has a Wi-Fi connection and clicks the button, the request is sent normally, but it sends two requests instead of one. My theory is that there is some kind of "request queue" and the first request (which has not been sent) remains in that queue, and when the second request is executed, the packet sends two requests instead of one. I'll really appreciate any kind of help and thanks for your time!

I tried some validations to reject similar requests but it doesn't work. I need to "clear" that queue and whenever the package needs to make a request send one instead of multiple requests.

This is how i make POST requests:

Future<dynamic> _postJsonData(
  String token, String endpoint, dynamic object) async {
try {
  final url = Uri.https(_urlBase, endpoint);
  final response = await http.post(
    url,
    body: object.toJson(),
    headers: {
      'Authorization': 'Bearer ' + token,
      'Content-Type': 'application/json'
    },
  );
  return response;
} catch (e) {
  return http.Response(
      "{'error': true, 'mensaje': 'Ocurrió un problema en el servidor, intente nuevamente más tarde'}",
      503);
}

}

5
  • try closing the http client if network disconnects by executing client.close(); Commented Nov 3, 2023 at 12:41
  • Hi @Prashant, i don't understand what you're trying to say, do you have an example to see? Commented Nov 3, 2023 at 13:07
  • Here you can get the connection status and check if connection exits then and only then send request and if not connection then give error and reopen button might be this link will work for you pub.dev/packages/queue Commented Nov 3, 2023 at 13:55
  • Hi @Riddhi, i'll do some research about the package and some tests, but with this is sure that will send only one request? Commented Nov 3, 2023 at 13:59
  • I think so but as per the code I think it'll clear/close all the queue Commented Nov 3, 2023 at 14:00

1 Answer 1

0

I would approach the issue in one of 2 ways

  1. Timeout: http has a timeout extension e.g. http.post(...).timeout(Duration(seconds:1), onTimeOut: ()=>throw Exception()) http timeout the idea here is that you expect the post to have a response in within that timeframe, if not, then assume something is wrong and throw an exception, ideally a custom exception that you can catch and decide what action you want to do with that. Here is an example of a custom network expection:

    class CustomNetworkException implements Exception { @override String toString(){ return 'timeout error'; } }

  2. have an interceptor. a bit more complicated, so I'd advise just looking at some articles instead of me trying to explain it here. checkout inteceptors, so when ever you get no-connection, you reset the call entirely

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.