Connection closed while receiving data(image through NetworkImage) flutter while retriving data from localhost

422

You might be missing Keep-Alive header in your http request.

Try adding this to your request headers:

  "Connection": "Keep-Alive",
Share:
422
Purvekta
Author by

Purvekta

Updated on January 03, 2023

Comments

  • Purvekta
    Purvekta over 1 year

    While retrieving the image file from network http://10.0.2.2:8000 localhost I got the following error:
    Connection closed while receiving data.

    I have an error when I call data (images only) from the URL.
    Note that I am using a local server.
    I’m trying to put the image inside Image.network(), this is image code.

    Image.network(urllink)
    

    The same URL works properly in the localhost browser and postman also.
    I am trying to retrieve images in the decoration box widget.
    The same url and image work when I paste direct url in the network image function but an error occurred when I tried to fetch the photoname through the apis. The photoname and APIs I am getting are working fine. There is not any problem with it.

     child: FutureBuilder<MatrimonyProfileData>(
                      future: futureMData,
                      builder: (context, snapshot) {
                        if (snapshot.hasData) {
                          // String matrimonyUrl = '/$matrimonyprofileUrl';
                          String photoName = snapshot.data!.matrimonies.photo;
    
                          print(photoName);
                          //var fullUrl = _url + apiUrl
    
                          var url;
                          // urllink = 'http://10.0.2.2:8000/matrimony/profile.jpg';
    
                          **url = (webLink + matrimony + photoName);
                          print(url);
                          String urllink = Url;**
                          return Column(
                            children: [
                              Container(
                                color: Colors.deepPurpleAccent.shade100,
                                child: Column(
                                  children: <Widget>[
                                    Container(
                                      margin: const EdgeInsets.all(15.0),
                                      padding: const EdgeInsets.all(3.0),
                                      decoration: BoxDecoration(
                                          border: Border.all(
                                              color: Colors.black, width: 3)),
                                      child: Wrap(
                                        // mainAxisAlignment: MainAxisAlignment.end,
                                        // direction: Axis.vertical,
                                        children: <Widget>[
                                          
                                    Container(
                                      height: 300,
                                      width: 400,
                                      //color: Colors.amber,
                                      child: Row(
                                        children: <Widget>[
                                          Container(
                                              height: 300,
                                              width: 170,
                                              //color: Colors.amber,
                                              decoration: BoxDecoration(
                                                image: DecorationImage(
                                                    image: **NetworkImage(urllink),**
    
                                                    
                                                    fit: BoxFit.fitWidth),
                                                
                                              )),
                                          // SizedBox(
                                          //   width: 20,
                                          // ),
    

    same problem on this link also https://askandroidquestions.com/2021/07/12/flutter-exception-connection-closed-while-receiving-data/

    • Muhammad Hussain
      Muhammad Hussain over 2 years
      Share your http request code from which you are retrieving this network url?
    • Purvekta
      Purvekta about 2 years
      class Network { final String _url = siteUrl; var token; _getToken() async { return token; } getData(apiUrl) async { return await http.get(Uri.parse(fullUrl), headers: _setHeaders()); } _setHeaders() => { 'Content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer $token', "Connection": "Keep-Alive", };
  • Purvekta
    Purvekta about 2 years
    already done but not succeeded