How do I solve the LateInitializationError in Flutter?

Hey guys i am havin the following problem and i hope someone can help me: I am trying to include the camera in my flutter app by using the following code:

import ‘dart:async’;
import ‘package:flutter/material.dart’;
import ‘package:camera/camera.dart’;

class Route02 extends StatefulWidget {
CameraState createState() => CameraState();

class CameraState extends State {
late List cameras;
late CameraController _controller;
bool isReady = false;

void initState() {

Future setupCameras() async {
try {
cameras = await availableCameras();
_controller = new CameraController(cameras[0], ResolutionPreset.ultraHigh);
await controller.initialize();
} on CameraException catch () {
setState(() {
isReady = false;
setState(() {
isReady = true;

Widget build(BuildContext context) {
if (!isReady && !_controller.value.isInitialized) {
return Container();
return AspectRatio(
aspectRatio: _controller.value.aspectRatio,
child: CameraPreview(_controller));

When accessing the camera part within the app the following error can be seen and afterwards the camera will start anyway:

LateInitializationError: Field ‘controller’ has not been initialized.

I have already tried to add the whenComplete() method and used ? but it didnt work either.

Does anyone have an idea?

Hello, I think it should be

with the underscore. Hth

Hey i already fixed that and it wasnt that. Still get the same error