Thanh Nguyen
Thanh Nguyen

Reputation: 319

image_collapse flutter package throw Exception "Looking up a deactivated widget's ancestor is unsafe"

I am using package image_collapse to display gallery images on my flutter mobile app. Currently it throw exception below :

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

Steps to reproduce

1.Open any image.

  1. Press the button to return to the previous screen

=> Throw exception

I tried follow some previous question to fix it. But they are not useful for me now ! Currently it seems to be working fine. There are no problems such as application crashes, etc. Except the error displayed in the debug console is quite annoying! Would be nice if someone could help. Thank for any helped !

GitHub of this package: https://github.com/ThuyenPV/image_collapse/blob/master/lib/src/widgets/gallery_app_bar.dart

Looking up a deactivated widget's ancestor is unsafe

I'm getting "Looking up a deactivated widget's ancestor is unsafe." Error

Looking up a deactivated widget's ancestor is unsafe. Navigator.of(context).pushAndRemoveUntil

My code :

import 'package:flutter/cupertino.dart';
import 'package:image_collapse/image_collapse.dart';

class GallerySection extends StatefulWidget {
  const GallerySection({super.key});

  @override
  State<GallerySection> createState() => _GallerySectionState();
}

class _GallerySectionState extends State<GallerySection> {
  static const List<String> listOfUrls = [
    "https://img.pikbest.com/png-images/plane-flying-on-location-and-map-with-forest-element_1493374.png!bw340",
    "https://img.pikbest.com/05/81/63/10VpIkbEsT5s3.jpg-0.jpg!bw340",
    "https://img.pikbest.com/01/42/43/44apIkbEsT5gu.jpg-0.jpg!bw340",
    "https://img.pikbest.com/pikbest/pic/00/32/88/08I888piCYQN.jpg-0.jpg!bw340",
    "https://img.pikbest.com/pikbest/pic/00/32/88/08I888piCYQN.jpg-0.jpg!bw340",
  ];
  @override
  Widget build(BuildContext context) {
    return  Center(
      child: Builder(
        builder: (context) {
          return const ImageCollapse(
            imageUrls: listOfUrls,
          );
        }
      ),
    );
  }
}

plant detail widget

class PlantDetailScreen extends StatefulWidget {
  final Plant plant;

  const PlantDetailScreen({super.key, required this.plant});

  @override
  State<PlantDetailScreen> createState() => _PlantDetailScreenState();
}

class _PlantDetailScreenState extends State<PlantDetailScreen> {
  final ScrollController _scrollController = ScrollController();
  bool _showButton = false;
  double sectionSpacing = 20;
  double contentSpacing = 10;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(() {
      if (_scrollController.position.pixels > 150 && !_showButton) {
        setState(() {
          _showButton = true;
        });
      } else if (_scrollController.position.pixels < 150 && _showButton) {
        setState(() {
          _showButton = false;
        });
      }
    });
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: Theme.of(context).colorScheme.onBackground,
        elevation: 0,
        title: Text(
          widget.plant.name,
          style: Theme.of(context)
              .textTheme
              .headlineSmall!
              .copyWith(color: Colors.white, fontWeight: FontWeight.bold),
        ),
        leading: IconButton(
          icon: const Icon(
            Icons.arrow_back,
            color: Colors.white,
          ),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      extendBodyBehindAppBar: false,
      body: SingleChildScrollView(
        controller: _scrollController,
        child: Column(
          children: [
            
            Align(
              alignment: Alignment.bottomCenter,
              child: Container(
                decoration: BoxDecoration(
                  color: Theme.of(context).colorScheme.onBackground,
                ),
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      //some code
                      const GallerySection(), //gallery images here
                      SizedBox(height: sectionSpacing),
                      //some code
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: _showButton
          ? FloatingActionButton(
              backgroundColor: const Color.fromRGBO(138, 140, 139, 1),
              child: Icon(
                FontAwesomeIcons.arrowUp,
                color: Theme.of(context).colorScheme.onPrimary,
              ),
              onPressed: () {
                _scrollController.animateTo(
                  0.0,
                  curve: Curves.easeOut,
                  duration: const Duration(milliseconds: 300),
                );
              },
            )
          : null,
    );
  }
}

Current screen

enter image description here

enter image description here

Upvotes: 0

Views: 48

Answers (0)

Related Questions