skip to Main Content

I am building an app in flutter, and I am trying to open the camera for preview. I keep getting the error LateInitializationError: Field _cameraController I do not see why it’s happening. my code is fairly small and I do not see why it’s complaining.


late List<CameraDescription> cameras;

class AddPostPage extends StatefulWidget {
  const AddPostPage({super.key});

  @override
  _AddPostPageState createState() => _AddPostPageState();
}

class _AddPostPageState extends State<AddPostPage> {
  late CameraController _cameraController;

  @override
  void initState() {
    availableCameras().then((value) {
      cameras = value;
      if (cameras.isEmpty) {}
      _initCameraController(cameras[0]).then((void v) {});
    });
    super.initState();
  }

  Future _initCameraController(CameraDescription cameraDescription) async {
    await _cameraController.dispose();
    _cameraController =
        CameraController(cameraDescription, ResolutionPreset.high);

    _cameraController.addListener(() {
      if (mounted) {
        setState(() {});
      }
    });
    try {
      await _cameraController.initialize();
    } on Exception catch (exception) {
      print(exception);
    }

    if (mounted) {
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return ResponsiveBuilder(
      builder: (BuildContext context, SizingInformation sizingInformation) {
        return Scaffold(
            body: Stack(
          children: [
            Container(
              height: double.infinity,
              child: CameraPreview(_cameraController),
            )
          ],
        ));
      },
    );
  }

  @override
  void dispose() {
    _cameraController.dispose();
    super.dispose();
  }
}

It’s happening at runtime when pressing on the button to access the camera in my app. I am using a iOS simulator

Any idea why ?

2

Answers


  1. may be you try to check if the _cameraController.value.isInitialized before loading camerapreview

    @override   Widget build(BuildContext context) {
        return ResponsiveBuilder(
          builder: (BuildContext context, SizingInformation sizingInformation) {
            return Scaffold(
                body: Stack(
              children: [
                Container(
                  height: double.infinity,
                  child:  (_cameraController!=null&&_cameraController.value.isInitialized)? 
                         CameraPreview(_cameraController):
                         Center(
                          child: CircularProgressIndicator()
                          ),),
              ],
            ));
          },
        );
    }
    
    Login or Signup to reply.
  2. The _cameraController isn’t initialized when you are loading the CameraPreview is the likely culprit. I’d change _cameraController to a nullable instead.

    In build do if _cameraController == null return an indicator or another widget, e.g. Text('Loading ...)`

    Then once the _cameraController is set in SetState it will rebuild, not be null and should load.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search