Dhruv Gupta
Dhruv Gupta

Reputation: 1

What is the problem in the following Flutter code related to camera and CameraController, how to solve the late init problem?

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

  @override
  State<MyCamera> createState() {
    return _MyCameraState();
  }
}

class _MyCameraState extends State<MyCamera> {
  late List<CameraDescription> cameras;
  late CameraDescription camera;
  late CameraController _controller;
  late Future<void> _initializeControllerFuture;
  late XFile image;

  @override
  void initState() {
    super.initState();
    requestPermission();
    initCamera().then((cam) {
      initController(cam);
    });
  }

  Future<CameraDescription> initCamera() async {
    cameras = await availableCameras();
    camera = cameras.first;
    return camera;
  }

  void initController(CameraDescription cam) {
    _controller = CameraController(
      // Get a specific camera from the list of available cameras.
      cam,
      // Define the resolution to use.
      ResolutionPreset.medium,
    );
    _initializeControllerFuture = _controller.initialize();
  }
}

late init error

i was expecting the code to work as dart analysis shows no error but instead i got this error when trying to run on a android device.

i know something is wrong in the way i wrote camera initializations but i do not know how to correct it.

can someone please help. thank you

Upvotes: 0

Views: 26

Answers (0)

Related Questions