'Flutter app crashs and restart from splash screen when using camera function

I want to click an image and send it to the server. the code works well if we are using gallery function but fails to work when it is swap by camera function.

I am able to open the camera, click the image and after this select if correct or wrong.

after this step the app crash. Next to this step, image needs to be sent to the cropping screen.

here is the code:

Future getImage() async {
    // var image = await ImagePicker.pickImage(source: ImageSource.gallery);
      var _image = await ImagePicker.pickImage(source: ImageSource.camera);
      croppedFile = await ImageCropper.cropImage(
      sourcePath: _image.path,
      aspectRatioPresets: [
        CropAspectRatioPreset.square,
        CropAspectRatioPreset.ratio3x2,
        CropAspectRatioPreset.original,
        CropAspectRatioPreset.ratio4x3,
        CropAspectRatioPreset.ratio16x9
      ],
      androidUiSettings: AndroidUiSettings(
          toolbarTitle: 'Cropper',
          toolbarColor: Colors.deepOrange,
          toolbarWidgetColor: Colors.white,
          initAspectRatio: CropAspectRatioPreset.square,
          lockAspectRatio: false),
      iosUiSettings: IOSUiSettings(
        minimumAspectRatio: 1.0,
      )
    );
    if (_pickInProgress) {
        return;
    }
      _pickInProgress = true;
      if (_image != null) {
        setState(() {
          _newAvatar = croppedFile;
        });
      }
      _pickInProgress = false;
  }

in Android Studio I have the bellow log when filter is used with keyword flutter:

2020-02-19 17:51:48.404 10631-10631/in.x.demo D/FlutterActivity: Using the launch theme as normal theme.
2020-02-19 17:51:48.413 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: Setting up FlutterEngine.
2020-02-19 17:51:48.413 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: No preferred FlutterEngine was provided. Creating a new FlutterEngine for this FlutterFragment.
2020-02-19 17:51:48.795 10631-10701/in.x.demo I/flutter: Observatory listening on http://127.0.0.1:38436/ayKKbiBs2ts=/
2020-02-19 17:51:49.031 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: Attaching FlutterEngine to the Activity that owns this Fragment.
2020-02-19 17:51:49.112 10631-10631/in.x.demo D/FlutterView: Attaching to a FlutterEngine: io.flutter.embedding.engine.FlutterEngine@178fa3d
2020-02-19 17:51:49.142 10631-10631/in.x.demo D/FlutterActivityAndFragmentDelegate: Executing Dart entrypoint: main, and sending initial route: /
2020-02-19 17:51:51.013 10631-10673/in.x.demo E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'contains' was called on null.
    Receiver: null
    Tried calling: contains("access_token")
    #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
    #1      _SplashScreenState.didChangeDependencies (package:demo/screens/common/splash_screen.dart:58:28)
    #2      _asyncThenWrapperHelper.<anonymous closure> (dart:async-patch/async_patch.dart:73:64)
    #3      _rootRunUnary (dart:async/zone.dart:1134:38)
    #4      _CustomZone.runUnary (dart:async/zone.dart:1031:19)
    #5      _FutureListener.handleValue (dart:async/future_impl.dart:139:18)
    #6      Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:680:45)
    #7      Future._propagateToListeners (dart:async/future_impl.dart:709:32)
    #8      Future._propagateToListeners (dart:async/future_impl.dart:607:9)
    #9      Future._completeWithValue (dart:async/future_impl.dart:524:5)
    #10     Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:554:7)
    #11     _rootRun (dart:async/zone.dart:1126:13)
    #12     _CustomZone.run (dart:async/zone.dart:1023:19)
    #13     _CustomZone.runGuarded (dart:async/zone.dart:925:7)
    #14     _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
    #15     _microtaskLoop (dart:async/schedule_microtask.dart:43:21)
    #16     _startMicrotaskLoop (dart:async/schedule_microtask.dart:52:5)
2020-02-19 17:51:51.015 10631-10673/in.x.demo I/flutter: genesis started
2020-02-19 17:51:51.106 10631-10673/in.x.demo I/flutter: device ID: ee44d5897844a106
2020-02-19 17:51:52.078 10631-10673/in.x.demo I/flutter: genesis final completed
2020-02-19 17:51:52.102 10631-10673/in.x.demo I/flutter: genesis api called !
2020-02-19 17:51:52.102 10631-10673/in.x.demo I/flutter: hasToken true
2020-02-19 17:51:53.085 10631-10673/in.x.demo E/flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.
    Receiver: null
    Tried calling: findAncestorStateOfType<NavigatorState>()
    #0      Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
    #1      Navigator.of (package:flutter/src/widgets/navigator.dart:1492:19)
    #2      _SplashScreenState.didChangeDependencies.<anonymous closure> (package:demo/screens/common/splash_screen.dart:54:23)
    #3      new Future.delayed.<anonymous closure> (dart:async/future.dart:316:39)
    #4      _rootRun (dart:async/zone.dart:1122:38)
    #5      _CustomZone.run (dart:async/zone.dart:1023:19)
    #6      _CustomZone.runGuarded (dart:async/zone.dart:925:7)
    #7      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:965:23)
    #8      _rootRun (dart:async/zone.dart:1126:13)
    #9      _CustomZone.run (dart:async/zone.dart:1023:19)
    #10     _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:949:23)
    #11     Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:23:15)
    #12     _Timer._runTimers (dart:isolate-patch/timer_impl.dart:384:19)
    #13     _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:418:5)
    #14     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)


`


Solution 1:[1]

  1. Your object is null when call '.contains('access_token')'
  2. Maybe your context is null when called 'navigator.of'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Angga Arya S