How to load network image with redirect url?

Viewed 903

i tried to load image from api, when i use NetworkImage from flutter or CachedNetworkImage plugin, the app frezee and stop working with this error

I/flutter ( 314): ══╡ EXCEPTION CAUGHT BY IMAGE RESOURCE SERVICE ╞════════════════════════════════════════════════════ I/flutter ( 314): The following _Exception was thrown resolving an image codec: I/flutter ( 314): Exception: HTTP request failed, statusCode: 308, I/flutter ( 314): http://example.com/image/sample.jpg

code sample :

CachedNetworkImage(
                          height: MediaQuery.of(context).size.width * 0.25,
                          width: MediaQuery.of(context).size.width * 0.25,
                          fit: BoxFit.cover,
                          placeholder: (context, url) => Image.asset(
                                "$assetsImages/loader_image.gif",
                                height:
                                    MediaQuery.of(context).size.width * 0.25,
                                width: MediaQuery.of(context).size.width * 0.25,
                              ),
                          errorWidget: (context, url, error) => Image.asset(
                                "$assetsImages/image_unavailable.jpg",
                                height:
                                    MediaQuery.of(context).size.width * 0.25,
                                width: MediaQuery.of(context).size.width * 0.25,
                              ),
                          imageUrl: restoImageUrl + value['filename'],
                        )

problem is on imageUrl: restoImageUrl + value['filename'], when i click the link image of error its opened normally on browser, how to fix this? or how to make flutter support that url 308 permanently redirect?

2 Answers

is your api working on the browser?

try with placeholder

FadeInImage.assetNetwork(
                                  placeholder: "assets/images/logo.png",
                                  image:
                                      "$_url/nativeapi/v1.0/s3object/GetCompanyLogo?siteId=${widget.siteId}")

Still in Sep '20 there isn't a good solution where you can configure your own Http module with Flutter and pass it on through Image.network() or NetworkImage().

There are a few libraries but they rather let you choose to followRedirects: boolean or add headers: Map<String, String> so you have to choose if you want Authentication or ability to redirection.

In my case, I was the owner of the API that was providing the redirection of the image. I realised I was using PERMANENT_REDIRECT (308) and it was working fine with Postman or browser, but what I did is changing the status code to MOVED_PERMANENTLY (301).

And it works now!

Related