Flutter & Navigator & ImagePicker : 为啥我可以进入下一页?查找已停用小部件的祖先是不安全的

Posted

技术标签:

【中文标题】Flutter & Navigator & ImagePicker : 为啥我可以进入下一页?查找已停用小部件的祖先是不安全的【英文标题】:Flutter & Navigator & ImagePicker : Why can I proceed to the next page? Looking up a deactivated widget's ancestor is unsafeFlutter & Navigator & ImagePicker : 为什么我可以进入下一页?查找已停用小部件的祖先是不安全的 【发布时间】:2021-01-11 11:29:18 【问题描述】:

这是我的 getImage 函数。拍照后,我会裁剪它。然后我转到下一页。它工作得很好。但是今天,我得到了一个错误。这是为什么?我无法理解。我应该更新我的颤振还是某事?

getImage(ImageSource source, BuildContext context) async 
    this.setState(() 
      _inProcess = true;
    );

    // File image = await ImagePicker.pickImage(source: source);
    final _picker = ImagePicker();
    PickedFile image = await _picker.getImage(source: source);
    // File file_image = File(image.path);

    if (image != null) 
      // Remove crop attribute if we don't want to resize the image
      File cropped = await ImageCropper.cropImage(
        sourcePath: image.path,
        aspectRatio: CropAspectRatio(ratioX: 1, ratioY: 1),
        compressQuality: 100, // 100 means no compression
        maxWidth: 700,
        maxHeight: 700,
        compressFormat: ImageCompressFormat.jpg,
        androidUiSettings: AndroidUiSettings(
          toolbarColor: primaryColor,
          toolbarTitle: "RPS Cropper",
          statusBarColor: primaryColor,
          backgroundColor: Colors.white,
          //toolbarWidgetColor: primaryColor,
          activeControlsWidgetColor: primaryColor,
          //dimmedLayerColor: primaryColor,
          cropFrameColor: primaryColor,
          cropGridColor: primaryColor,
        ),
      );

      print('======= After Cropped =======');

      this.setState(() 
        _selectedFile = cropped;
        //_selectedFile = File(image.path);
        _inProcess = false;
        //_showDelete = true;
        isTaken = true;
      );

      if (_selectedFile != null) 
        print('====== Leaving Image Evaluation =====');
        // Navigator.pop(context);
        this.setState(() 
          _inProcess = false;
        );
        await Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (context) => UploadPictureInfo(
              image: _selectedFile,
            ),
          ),
        );
        print('After pop and push Image Evaluation');
       else 
        //Navigator.of(context).pop();
        this.setState(() 
          //_selectedFile = cropped;
          _inProcess = false;
          // //_showDelete = true;
          // isTaken = true;
        );
      
     else 
      this.setState(() 
        _inProcess = false;
      );
    
  

这是错误信息。

I/BitmapCropTask( 9329): Should crop: true
I/flutter ( 9329): ======= After Cropped =======
I/flutter ( 9329): ====== Leaving Image Evaluation =====
E/flutter ( 9329): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 9329): At this point the state of the widget's element tree is no longer stable.
E/flutter ( 9329): 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.
E/flutter ( 9329): #0      Element._debugCheckStateIsActiveForAncestorLookup.<anonymous closure> (package:flutter/src/widgets/framework.dart:3904:9)
E/flutter ( 9329): #1      Element._debugCheckStateIsActiveForAncestorLookup (package:flutter/src/widgets/framework.dart:3918:6)
E/flutter ( 9329): #2      Element.findAncestorStateOfType (package:flutter/src/widgets/framework.dart:4037:12)
E/flutter ( 9329): #3      Navigator.of (package:flutter/src/widgets/navigator.dart:2216:40)
E/flutter ( 9329): #4      Navigator.pushReplacement (package:flutter/src/widgets/navigator.dart:1876:22)
E/flutter ( 9329): #5      _ImageEvaluationState.getImage (package:PhotoEarn/screens/camera/image_evaluation.dart:199:25)
E/flutter ( 9329): <asynchronous suspension>
E/flutter ( 9329): #6      _ImageEvaluationState.build.<anonymous closure>.<anonymous closure> (package:PhotoEarn/screens/camera/image_evaluation.dart:474:33)
E/flutter ( 9329): #7      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:993:19)
E/flutter ( 9329): #8      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:1111:38)
E/flutter ( 9329): #9      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:183:24)
E/flutter ( 9329): #10     TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:598:11)
E/flutter ( 9329): #11     BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:287:5)
E/flutter ( 9329): #12     BaseTapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:222:7)
E/flutter ( 9329): #13     PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:476:9)
E/flutter ( 9329): #14     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:77:12)
E/flutter ( 9329): #15     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:122:9)
E/flutter ( 9329): #16     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:377:8)
E/flutter ( 9329): #17     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:120:18)
E/flutter ( 9329): #18     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:106:7)
E/flutter ( 9329): #19     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:370:19)
E/flutter ( 9329): #20     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:350:22)
E/flutter ( 9329): #21     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:266:11)
E/flutter ( 9329): #22     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:305:7)
E/flutter ( 9329): #23     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:240:7)
E/flutter ( 9329): #24     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:213:7)
E/flutter ( 9329): #25     _rootRunUnary (dart:async/zone.dart:1206:13)
E/flutter ( 9329): #26     _CustomZone.runUnary (dart:async/zone.dart:1100:19)
E/flutter ( 9329): #27     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1005:7)
E/flutter ( 9329): #28     _invoke1 (dart:ui/hooks.dart:265:10)
E/flutter ( 9329): #29     _dispatchPointerDataPacket (dart:ui/hooks.dart:174:5)
E/flutter ( 9329): 
D/EGL_emulation( 9329): eglMakeCurrent: 0xddd058e0: ver 3 0 (tinfo 0xdcaa8ec0)
D/EGL_emulation( 9329): eglMakeCurrent: 0xddd19e80: ver 3 0 (tinfo 0xdcab1c40)
D/EGL_emulation( 9329): eglMakeCurrent: 0xddd19e80: ver 3 0 (tinfo 0xdcab1c40)
D/EGL_emulation( 9329): eglMakeCurrent: 0xddd058e0: ver 3 0 (tinfo 0xdcaa8ec0)
D/eglCodecCommon( 9329): setVertexArrayObject: set vao to 0 (0) 53 0
D/EGL_emulation( 9329): eglMakeCurrent: 0xddd19e80: ver 3 0 (tinfo 0xdcab1c40)

请看我的代码!期待您的回音。非常感谢!

【问题讨论】:

【参考方案1】:

我找到了解决办法。

我使用了这种类型的导航器。

Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (context) => UploadPictureInfo(
              image: _selectedFile,
            ),
          ),
        );

【讨论】:

以上是关于Flutter & Navigator & ImagePicker : 为啥我可以进入下一页?查找已停用小部件的祖先是不安全的的主要内容,如果未能解决你的问题,请参考以下文章

flutter系列之:在flutter中使用导航Navigator

在 Flutter 的嵌套 Navigator 结构中,如何获取特定的 Navigator?

Navigator.push 没有动画 - Flutter

Flutter:Navigator2.0介绍及使用

Flutter Navigator 2.0原理详解

在 Flutter 中使用 Navigator.push() 时出现黑屏