FTF Studio
FTF Studio

Reputation: 99

error Looking up a deactivated widget's ancestor is unsafe

I don't know what is wrong with this, but when I click on the close icon button I get this error.

════════ Exception caught by widgets library ═══════════════════════════════════ The following assertion was thrown while finalizing the widget tree: Looking up a deactivated widget's ancestor is unsafe.

At this point the state of the widget's element tree is no longer stable.

To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

this is my code

// ...
class ImageViewer extends StatefulWidget {
  List<String> imageList;
  int currentIndex;
  BoxDecoration backgroundDecoration;
  ImageViewer(
      {Key key,
      this.imageList,
      this.backgroundDecoration,
      this.currentIndex = 0})
      : super(key: key);

  @override
  _ImageViewerState createState() => _ImageViewerState();
}

class _ImageViewerState extends State<ImageViewer> {
  PageController _pageController;

  @override
  void initState() {
    // TODO: implement initState
    _pageController = PageController(initialPage: widget.currentIndex);
    super.initState();
  }

  @override
  void dispose() {
    // TODO: implement dispose
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: ColorfulBackground(
      bottomColor: Colors.lightBlue[400],
      middleColor: Colors.purple,
      body: Stack(children: [
        PhotoViewGallery.builder(
          scrollPhysics: const BouncingScrollPhysics(),
          builder: (BuildContext context, int index) {
            return PhotoViewGalleryPageOptions(
              imageProvider: CachedNetworkImageProvider(
                widget.imageList[index],
                cacheManager: CustomCacheManager.instance,
              ),
              tightMode: true,
              maxScale: PhotoViewComputedScale.contained * 2.5,
              gestureDetectorBehavior: HitTestBehavior.translucent,
              minScale: PhotoViewComputedScale.contained * 1.0,
              initialScale: PhotoViewComputedScale.contained * 1.0,
              heroAttributes:
                  PhotoViewHeroAttributes(tag: widget.imageList[index]),
            );
          },
          itemCount: widget.imageList.length,
          backgroundDecoration: BoxDecoration(
            color: Colors.transparent,
          ),
          onPageChanged: (index) {
            setState(() {
              widget.currentIndex = index;
            });
          },
          loadingBuilder: (context, event) => Center(
            child: Container(
              width: 20.0,
              height: 20.0,
              child: CircularProgressIndicator(
                value: event == null
                    ? 0
                    : event.cumulativeBytesLoaded / event.expectedTotalBytes,
              ),
            ),
          ),
          // backgroundDecoration: widget.backgroundDecoration,
          pageController: _pageController,
          // onPageChanged: onPageChanged,
        ),

        // close icon button //////////////////////////////////

        Positioned(
          top: 40,
          left: 10,
          child: IconButton(
              onPressed: () {
                Navigator.of(context).pop();
              },
              icon: Icon(
                Icons.close_rounded,
                color: kblackcolor,
              )),
        ),
        Positioned(
            right: 40,
            bottom: 100,
            child: Container(
              padding: EdgeInsets.symmetric(horizontal: 20, vertical: 10),
              decoration: BoxDecoration(
                  color: kPrimaryColor,
                  borderRadius: BorderRadius.circular(20)),
              child: Text(
                "${widget.currentIndex + 1}/ ${widget.imageList.length}",
                style:
                    TextStyle(color: kwhitecolor, fontWeight: FontWeight.w500),
              ),
            )),
      ]),
    ));
  }
}


Upvotes: 2

Views: 2232

Answers (2)

PeakGen
PeakGen

Reputation: 23035

The new updated version of photo_view can sort out the issue. By the time I am writing this, the latest version is 0.14.0

Upvotes: 0

The problem is coming from photo_view package.

In pubspec.yaml remove photo_view from dependecies

dependencies:
  photo_view: ^0.13.0

Add:

dependency_overrides:
  photo_view:
    git:
      url: https://github.com/bluefireteam/photo_view
      ref: master

This way you will avoid errors in dependencies that come from the same version.

Upvotes: 4

Related Questions