How to get redirect URL in dart/flutter?

不羁的心 提交于 2021-01-04 05:53:15

问题


I'm building an app that shows an image taken from source.unsplash. My request url is:

https://source.unsplash.com/random/`$widthx$height`

Then the service redirects to some specific image url picked fitting my width and height.

I can show the image with:
Image.network(url above),
but I cannot access the new redirected url for some other parts of my code which is really necessary. Is there a way to get it?

Thanks in advance

Something I'm searching for is like

Http.Response response = await Http.get(url);
print(response.finalRedirectURL);

回答1:


It would be better if you use api.unsplash.com and not source.

If followRedirects is true, there's no way according to the docs to get the final redirected URL, so maybe post a github issue for this. See: https://pub.dev/documentation/http/latest/http/Response-class.html

The only way I see this happening is making followRedirects: false, will give you a response headers with location as a key that gives you the next redirect. You'd have to write a loop to keep going till the status code changes from 302 to something else.

Hope this helps.




回答2:


For my own project, I was also not able to find any method from flutter/dart side.

However, I was able to find the location in response.headers. Maybe this can work.



来源:https://stackoverflow.com/questions/56861837/how-to-get-redirect-url-in-dart-flutter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!