我已经获得了网络图像并使用了三元运算符,我可以在其中显示网络图像但无法显示默认资产图像,其中网络图像在网格中无效默认图像也是如此-
我是新来的颤振
image: NetworkImage(products[index]
.productImageList[0]
)!= null
? NetworkImage(
products[index].productImageList[0])
:Image.asset("assets/defimg.jpg"),
fit: BoxFit.fitHeight ),
它在索引 0 上显示网络图像,但不加载网络图像无效或为空的资产图像并抛出此错误-
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data
When the exception was thrown, this was the stack:
#0 _futurize (dart:ui/painting.dart:5230:5)
#1 ImageDescriptor.encoded (dart:ui/painting.dart:5098:12)
#2 instantiateImageCodec (dart:ui/painting.dart:1998:60)
<asynchronous suspension>
在此之后,我尝试了 try/catch -我的带有 try catch 的代码是——
child: Builder(builder: (BuildContext context) {
try {
return Center(
child: Image.network(
products[index]
.productImageList[0],
fit: BoxFit.contain,
),
);
} catch (e) {
print("Got exception ${e.toString()}");
return Image.asset("assets/defimg.jpg", fit: BoxFit.contain);
}
}),
给我和上面一样的错误..
图像响应来自列表“productImageList”中的 api,如下所示——
"sellingPrice": "4000",
"productImageList": [
"https://www.xyzz.com/postadsimages/img/23682201.jpg"
],
"createdDate": 1607754473000,