小编Ram*_*aha的帖子

try/catch 不适用于网络图像(例外:图像数据无效)

我已经获取网络图像并使用三元运算符,我可以在其中显示网络图像,但无法显示默认资产图像,其中网络图像在网格中默认图像无效,也如此-

\n

我是颤振新手

\n
 image: NetworkImage(products[index]\n                                              .productImageList[0]\n                                  )!= null\n                                      ? NetworkImage(\n                                      products[index].productImageList[0])\n                                      :Image.asset("assets/defimg.jpg"),\n                                  fit: BoxFit.fitHeight ),\n
Run Code Online (Sandbox Code Playgroud)\n

它在索引 0 上显示网络图像,但不会加载网络图像无效或为空的资产图像并抛出此错误 -

\n
\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90 Exception caught by image resource service \xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following _Exception was thrown resolving an image codec:\nException: Invalid image data\n\nWhen the exception was thrown, this was the stack: \n#0      _futurize (dart:ui/painting.dart:5230:5)\n#1      ImageDescriptor.encoded (dart:ui/painting.dart:5098:12)\n#2      instantiateImageCodec (dart:ui/painting.dart:1998:60)\n<asynchronous suspension>\n
Run Code Online (Sandbox Code Playgroud)\n

之后我尝试了 try/catch -我的 try catch 代码是 --

\n
child: Builder(builder: (BuildContext context) {\n                              try {\n                                return Center(\n                                  child: Image.network(\n                                      products[index]\n …
Run Code Online (Sandbox Code Playgroud)

try-catch conditional-operator networkimageview flutter-dependencies flutter-layout

10
推荐指数
1
解决办法
3万
查看次数