Flutter: Connection closed while receiving data

8,784

Solution 1

Keep-Alive header in the headers of your request might be missing, please check with APIs required headers

Solution 2

I had the issue with a get request, sometimes I get the response from the server and sometimes I receive the exception Connection closed while receiving data on same button click event. Then I founded that the number of records of particular table in database is quite large. My suggested solution is to use pagination in the backend server.

Share:
8,784
Nabeel Ashfaq
Author by

Nabeel Ashfaq

Updated on December 22, 2022

Comments

  • Nabeel Ashfaq
    Nabeel Ashfaq over 1 year

    I'm calling post API, and sometimes I get the response from the server and sometimes I receive the exception Connection closed while receiving data. Request is same in both cases, and according to backend server logs, a response is sent but i didn't receive it. I have this issue both in simulator and actual device.

    try {
      final result =
          await http.post(url, body: encodedBody, headers: apiHeader);
      Map<String, dynamic> response = json.decode(result.body);
    
      print("Response: $response");
    
      return response;
    } catch (error) {
      Map<String, dynamic> response = Map<String, dynamic>();
      response['success'] = false;
      response['message'] = error;
      return response;
    }
    
  • Fakhar Ilyas
    Fakhar Ilyas about 2 years
    bro, I am still facing this issue and I also try adding this Keep-Alive in the header section still don't work! if you can help please
  • Yadu
    Yadu about 2 years
    Check if you're correctly adding it
  • Fakhar Ilyas
    Fakhar Ilyas about 2 years
    yes i am adding it ina correct way 'Connection': 'Keep-Alive', 'connection': 'keep-alive', i tried in both ways lower case and upercase