Batu
Batu

Reputation: 31

Exception Caught By Widgets Library when coming back from a page

I createad a page to show network images in full screen which is also zoomable. Also I am using PaletteColor to get dominant color from the url image to set it for the background color. The page works fine, however, when I clicked to back button it throws an error which says: "Exception caught by widgets library Looking up a deactivated widget's ancestor is unsafe."

Packages I am using to create this screen are: palette_generator 0.3.3, photo_view 0.13.0, cached_network_image 3.2.0

I couldn't handle the problem.

MyCode

class ImageZoom extends StatefulWidget {
  final String imageUrl;
  const ImageZoom({
    Key? key,
    required this.imageUrl,
  }) : super(key: key);

  @override
  State<ImageZoom> createState() => _ImageZoomState();
}

class _ImageZoomState extends State<ImageZoom> {
  late PaletteColor color;
  bool isColorInit = false;

  @override
  void initState() {
    super.initState();
    _updatePalette();
  }

  _updatePalette() async {

    final PaletteGenerator generator = await PaletteGenerator.fromImageProvider(
      NetworkImage(widget.imageUrl),
      //size: const Size(500, 100), // To make it more efficient
    );

    setState(() {
      color = (generator.lightMutedColor ??
          PaletteColor(CassettColors.primaryRed, 2));
      isColorInit = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (isColorInit) {
      return Scaffold(
        appBar: buildAppBar(context),
        backgroundColor: color.color,
        body: Center(
          child: CachedNetworkImage(
            imageUrl: widget.imageUrl,
            imageBuilder: (context, imageProvider) => PhotoView(
              enablePanAlways: false,
              imageProvider: imageProvider,
              minScale: PhotoViewComputedScale.contained,
              maxScale:PhotoViewComputedScale.covered * 1.8,
              initialScale: PhotoViewComputedScale.contained,
              basePosition: Alignment.center,
              backgroundDecoration: BoxDecoration(color: color.color),
              ),
            placeholder: (context, url) => const CircularProgressIndicator(
              color: CassettColors.primaryRed,
            ),
          ),
        ),
      );
    } else {
      return  Scaffold(
        appBar: buildAppBar(context),
        backgroundColor: CassettColors.background,
        body: const Center(
            child: CircularProgressIndicator(
          color: CassettColors.primaryRed,
        )),
      );
    }
  }

  AppBar buildAppBar(BuildContext context) => AppBar(
        backgroundColor: CassettColors.seconBackground,
        leading: InkResponse(
          radius: 16,
          highlightColor: CassettColors.primaryRed,
          splashColor: CassettColors.primaryRed,
          onTap: () {
            Navigator.pop(context);
          },
          child: Center(
            child: Icon(Icons.arrow_back_ios_new_outlined,
                color: CassettColors.text100),
          ),
        ),
      );
}

When I press the back button from this page I get:

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

When the exception was thrown, this was the stack
#0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure>
package:flutter/…/widgets/framework.dart:4187
#1      Element._debugCheckStateIsActiveForAncestorLookup
package:flutter/…/widgets/framework.dart:4201
#2      Element.getElementForInheritedWidgetOfExactType
package:flutter/…/widgets/framework.dart:4227
#3      TickerMode.getNotifier
package:flutter/…/widgets/ticker_provider.dart:96
#4      TickerProviderStateMixin._updateTickerModeNotifier
package:flutter/…/widgets/ticker_provider.dart:327
#5      TickerProviderStateMixin.createTicker
package:flutter/…/widgets/ticker_provider.dart:291
#6      new AnimationController
package:flutter/…/animation/animation_controller.dart:246
#7      PhotoViewCoreState._scaleAnimationController
package:photo_view/…/core/photo_view_core.dart:112
#8      PhotoViewCoreState._scaleAnimationController (package:photo_view/src/core/photo_view_core.dart)
package:photo_view/…/core/photo_view_core.dart:1
#9      PhotoViewCoreState.dispose
package:photo_view/…/core/photo_view_core.dart:274
#10     StatefulElement.unmount
package:flutter/…/widgets/framework.dart:5017
#11     _InactiveElements._unmount
package:flutter/…/widgets/framework.dart:1978
#12     _InactiveElements._unmount.<anonymous closure>
package:flutter/…/widgets/framework.dart:1976
#13     ComponentElement.visitChildren
package:flutter/…/widgets/framework.dart:4841
#14     _InactiveElements._unmount
package:flutter/…/widgets/framework.dart:1974
#15     _InactiveElements._unmount.<anonymous closure>
package:flutter/…/widgets/framework.dart:1976
#16     _LayoutBuilderElement.visitChildren
package:flutter/…/widgets/layout_builder.dart:71
#17     _InactiveElements._unmount
package:flutter/…/widgets/framework.dart:1974
#18     _InactiveElements._unmount.<anonymous closure>
package:flutter/…/widgets/framework.dart:1976

Upvotes: 1

Views: 357

Answers (1)

Brendan
Brendan

Reputation: 1167

This is a bug from the PhotoView package.

As a temporary fix, replace your installed version with this fork.

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

Upvotes: 1

Related Questions