如何在 dart/flutter 中获取重定向 URL?

Ale*_*tin 5 dart flutter

我正在构建一个应用程序,显示从 source.unsplash 获取的图像。我的请求网址是:

https://source.unsplash.com/random/`$widthx$height`
Run Code Online (Sandbox Code Playgroud)

然后该服务重定向到一些特定的图像网址,该网址选择适合我的宽度和高度。

我可以使用: 显示图像
Image.network(url above)
但我无法访问代码的其他部分的新重定向网址,这确实是必要的。有办法得到吗?

提前致谢

我正在寻找的东西就像

Http.Response response = await Http.get(url);
print(response.finalRedirectURL);
Run Code Online (Sandbox Code Playgroud)

ish*_*aan 8

如果您使用 api.unsplash.com 而不是 source 会更好。

如果followRedirects是 true,则根据文档无法获取最终的重定向 URL,因此可能为此发布一个 github 问题。请参阅: https: //pub.dev/documentation/http/latest/http/Response-class.html

我看到这种情况发生的唯一方法是制作followRedirects: false, 将为您提供一个响应标头,并location作为为您提供下一个重定向的键。您必须编写一个循环才能继续执行,直到状态代码从 302 更改为其他代码。

希望这可以帮助。