0

Inside my widget I have some images. These images have a position. I would like to check if an image with a certain position exists, show it. Else the child of my GestureDetector should simply be nothing.

This is what I tried:

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

But if there is no image with ImagePosition.ONE , I get a Bad State: No elemnt- Error.

What am I missing here?

Let me know if you need any more details!

Chris
  • 1,828
  • 6
  • 40
  • 108

2 Answers2

0

Try to update : null at the end with : SizedBox.shrink(). I think the problem is that you are giving a null as a child to the GestureDetector in case there is no image

dariowskii
  • 285
  • 2
  • 9
0

I'm not actually because not having all the stack trace of your error but try replacing null by empty Container() if still not work please provide all your stacktrace for further help