flutter - 错误 查找已停用小部件的祖先是不安全的

标签 flutter

我不知道这有什么问题,但是当我单击关闭图标按钮时,我收到此错误。

════════小组件库捕获异常═════════════════════════════ ═════ ═ 完成小部件树时抛出以下断言: 查找已停用小部件的祖先是不安全的。

此时小部件的元素树的状态不再稳定。

要在 widget 的 dispose() 方法中安全地引用其祖先,请通过在 widget 的 didChangeDependency() 方法中调用 dependentOnInheritedWidgetOfExactType() 来保存对祖先的引用。

这是我的代码

// ...
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),
              ),
            )),
      ]),
    ));
  }
}


最佳答案

问题来自 photo_view 包。

在 pubspec.yaml 中从依赖项中删除 photo_view

dependencies:
  photo_view: ^0.13.0

添加:

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

这样您就可以避免来自同一版本的依赖项中的错误。

关于flutter - 错误 查找已停用小部件的祖先是不安全的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71398645/

相关文章:

android - 离开页面时如何停止在 initState 中启动的 Timer.periodic

flutter - 为什么我在 flutter 中遇到这个错误?构造函数错误?

dart - Caret 符号在 Dart 中有什么作用

java - 尝试重新安装或更新您的 Android SDK Manager

Flutter - 将背景渐变应用于可 ScrollView

flutter - Flutter:从 future 函数返回int变量是无状态小部件:已编辑和更改

ios - 为设备构建时遇到错误 - flutter

flutter - 如何为 Flutter SliverList 中的元素设置动画?

flutter - Flutter文本小部件将中间的单词分解到下一行如何停止

flutter - "Could not connect to lockdownd"尝试运行 flutter 应用程序时