2

私はネットワークイメージを取得し、ネットワークイメージを表示できる三項演算子を使用しましたが、ネットワークイメージが無効なデフォルトのアセットイメージをグリッドに表示することはできません-

私はフラッターが初めてです

 image: NetworkImage(products[index]
                                              .productImageList[0]
                                  )!= null
                                      ? NetworkImage(
                                      products[index].productImageList[0])
                                      :Image.asset("assets/defimg.jpg"),
                                  fit: BoxFit.fitHeight ),

インデックス 0 にネットワーク イメージが表示されますが、ネットワーク イメージが無効または null のアセット イメージが読み込まれず、このエラーがスローされます。

════════ 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,

4

2 に答える 2