1

this flutter code used to work but after an update it does not work anymore and I absolutely have been struggling for hours on it. The error says

LateInitializationError: Field '_camera@26505073' has not been initialized.

Here is my Code

class _CameraFlowState extends State<CameraFlow> {
  // 2
  late CameraDescription _camera;
  bool _shouldShowCamera = false;

  int len = 0;

  // 3
  List<MaterialPage> get _pages {
    return [
      // Show Gallery Page
      MaterialPage(
          child: GalleryPage(
              shouldLogOut: widget.shouldLogOut,
              shouldShowCamera: () => _toggleCameraOpen(true))),
      // Show Camera Page
      if (_shouldShowCamera)
        MaterialPage(
            child: CameraPage(
                camera: _camera,
                didProvideImagePath: (imagePath) {
                  _toggleCameraOpen(false);
                }))
    ];
  }


  @override
  void initState() {
    super.initState();
    _getCamera();
  }

  @override
  Widget build(BuildContext context) {
    // 4
    return Navigator(
      pages: _pages,
      onPopPage: (route, result) => route.didPop(result),
    );
  }

  // 5
  void _toggleCameraOpen(bool isOpen) {
    setState(() {
      _shouldShowCamera = isOpen;
    });
  }

  Future<void> _getCamera() async {
    final camerasList = await availableCameras();
    setState(() {
      final firstCamera = camerasList.first;
      _camera = firstCamera;
    });
  }
}

I tried doing this

CameraDescription? _camera;

But then I get

Null check operator used on a null value.

How can I fix this problem? Please help me out...

gs97ahn
  • 175
  • 9
  • 1
    The error indicates that you are trying to use the `_camera` before it got any value. That problem is not solved by making it nullable or late. The problem must be fixed by ensuring your program can either handle the case of the variable being `null`, or make use of `FutureBuilder` to handle the async nature of that variable. – julemand101 Jan 24 '22 at 13:44
  • Visit this link: https://stackoverflow.com/questions/64278595/null-check-operator-used-on-a-null-value – Ali Punjabi Jan 24 '22 at 14:10

0 Answers0