Flutter & Navigator & ImagePicker : Why can I proceed to the next page? Looking up a deactivated widget's ancestor is unsafe

290

I found the solution.

I used this type of navigator instead.

Navigator.of(context).pushReplacement(
          MaterialPageRoute(
            builder: (context) => UploadPictureInfo(
              image: _selectedFile,
            ),
          ),
        );
Share:
290
Punreach Rany
Author by

Punreach Rany

Updated on December 24, 2022

Comments

  • Punreach Rany
    Punreach Rany over 1 year

    This is my getImage Function. After I take a picture, I will crop it. then I go to the next page. It was working just fine. But today, I got an error. Why is that? I can't understand. Should I update my flutter or sth?

    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;
          });
        }
      }
    

    Here is the error message.

    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)
    

    Please have a look at my code! I am looking forward to hearing from you.Thank you very much!