这是API:
"page": 1,
"results": [
{
"original_language": "en",
"original_title": "Mortal Kombat",
"popularity": 2309.097,
"poster_path": "/nkayOAUBUu4mMvyNf9iHSUiPjF1.jpg",
}
这是我尝试从中获取图像并将其保存在变量中的方式:
class MostPopular {
MostPopular({
this.image0,})
final String image0;
factory MostPopular.fromJson(Map<String, dynamic> json) {
final String image =
(json["results"] as List).map((j) => j["poster_path"]).toList()[0];
return MostPopular(
image0: image,)
}
}```
And when i run the app, it shows `/nkayOAUBUu4mMvyNf9iHSUiPjF1.jpg` and not the image. I tried changing the `String` but it still doesn't work and I'm not sure if thats the problem.
答案 0 :(得分:0)
您实际上从 api 获得的只是图像 path
。
但是,您仍然必须附加其域的实际基本网址。
来自他们的文档,
<块引用>如果为电影返回 /kqjL17yufvn9OVLyXYpvtyrFfak.jpg 的 poster_path 并且您正在寻找 w500 尺寸,则完整图像 URL 如下所示: https://image.tmdb.org/t/p/w500/kqjL17yufvn9OVLyXYpvtyrFfak.jpg
所以,把你的函数改成这个,
return MostPopular(image0: 'https://image.tmdb.org/t/p/original${image}')
当你想使用它时,你大概可以这样使用它,
Image.network(mostPopular.image0); // where mostPopular is an instance of MostPopular