How to handle HTTP api request while navigating pages quickly | FLUTTER | DART

1,601

I found a solution

To achieve this need to close the http connection when navigating, for do that need to make a client from http and need t close that client on dispose method

var client = http.Client()
var response = await client.get(url)

close the connecton when navigating

void dispose(){
  super.dispose();
  client.close()
}
Share:
1,601
s.am.i
Author by

s.am.i

Updated on November 28, 2022

Comments

  • s.am.i
    s.am.i over 1 year

    For my scenario, I have used flutter http package to make http requests...In home screen I have to send around 3 http requests, Since I had to use await requests are sending one by one.

    I have used BaseAPiService class so all the api calls will go though that,

    If I Navigate to another place while above request happening how to destroy that connection?? Otherwise if after navigate also app is waiting till previous Api requestes completed..

    sample base api service class used

    class ApiService {
      apiGet(url, data) async {
      Get.dialog(LoadingDialog());
      var response;
      if (data == null) {
        response = await http.get(
        baseUrl + url,
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
      );
    }
    Navigator.pop(Get.overlayContext);
    return response;
    }
    
    apiPost(url, data) async {
      FocusScopeNode currentFocus = FocusScope.of(Get.context);
      if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
      }
      Get.dialog(LoadingDialog());
      var response;
      if (data != null) {
       response = await http.post(baseUrl + url,
          headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json',
          },
          body: data);
    }
    if (data == null) {
      response = await http.post(
        baseUrl + url,
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
        },
      );
    }
    Navigator.pop(Get.overlayContext);
    return response;
    }
    }
    
    • LOLWTFasdasd asdad
      LOLWTFasdasd asdad over 3 years
      Could you provide some code? Hard to tell how to help you.
    • s.am.i
      s.am.i over 3 years
      sample base api service class added
    • LOLWTFasdasd asdad
      LOLWTFasdasd asdad over 3 years
      Could you also a little bit more specific what you want to achieve? You want to kill an established connection after switching the screen with the navigator?
    • LOLWTFasdasd asdad
      LOLWTFasdasd asdad over 3 years
      what about api.client.close(); ?
    • s.am.i
      s.am.i over 3 years
      Yeah, when researching how to kill http connection, I also come up with that solution. I'll check and give an update, Thank you for guiding me
    • s.am.i
      s.am.i over 3 years
      client.close() did the work
  • s.am.i
    s.am.i over 3 years
    Yes, If I navigate From home page without waiting for the http response, Still code is waiting for the homepage response to complete before sending new http response, I think it's because of await function, I need to kill the connection with http when navigating
  • Alexey
    Alexey almost 3 years
    That's an excellent solution! No need for a special API...