Flutter 仅在数组中存在图像时才显示图像

问题描述

在我的 widget 中,我有一些 images。这些 images一个 position。我想检查某个位置的图像是否存在,显示它。否则,我的 childGestureDetector 应该什么都没有。

这是我试过的:

child: GestureDetector(
        onTap: () => print("tapped 1"),onScaleStart: (details) {
          _startingFocalPoint.value = details.focalPoint;
          _prevIoUsOffset.value = _offset.value;
          _prevIoUsZoom.value = _zoom.value;
        },onScaleUpdate: (details) {
          _zoom.value = _prevIoUsZoom.value * details.scale;
          final Offset normalizedOffset =
              (_startingFocalPoint.value - _prevIoUsOffset.value) /
                  _prevIoUsZoom.value;
          _offset.value =
              details.focalPoint - normalizedOffset * _zoom.value;
        },child: widget.images.where(
                    (image) => image.position == ImagePosition.ONE) !=
                null
            ? ClipPath(
                clipper: _Position1ClipperImage(),child: Transform(
                  transform: Matrix4.identity()
                    ..translate(_offset.value.dx,_offset.value.dy)
                    ..scale(_zoom.value),child: Image.asset(
                      widget.images
                          .firstWhere(
                            (image) => image.position == ImagePosition.ONE,)
                          .path,width: widget.width,height: widget.width,fit: BoxFit.fill),),)
            : null,

但是如果没有 imageImagePosition.ONE ,我会收到 Bad State: No elemnt- 错误

在这里遗漏了什么?

如果您需要更多详细信息,请告诉我!

解决方法

尝试在最后用 : null 更新 : SizedBox.shrink()。我认为问题在于您将 null 作为 child 提供给 GestureDetector,以防没有图像

,

我实际上不是因为没有您的错误的所有堆栈跟踪,但尝试用空 Container() 替换 null 如果仍然无效,请提供您的所有堆栈跟踪以获取进一步帮助