Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter LateInitializationError controller has not been initialized

Im trying to create simple camera app in flutter and i run into this error. Whenever I switch to the ‘Scan'(Camera) page i get the following message LateInitializationError: Field '_initializeController@28060535' has not been initialized..From what i can see the error occures within the startCamera function. If i hot-reload the app, everything works as it should. Here’s my code
Main.dart (only the widgdet part)

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: PageView(
          controller: _controller,
          onPageChanged: _onPageChanged,
          children: const [
            ScanPage(title: 'scan'),
            SettingsPage(title: 'settings'),
          ],
        ),
        bottomNavigationBar: BottomNavigationBar(
            items: const <BottomNavigationBarItem>[
              BottomNavigationBarItem(icon: Icon(Icons.camera), label: 'Scan'),
              BottomNavigationBarItem(
                  icon: Icon(Icons.settings), label: 'Settings')
            ],
            selectedItemColor: Colors.white,
            currentIndex: _selectedIndex,
            onTap: _onItemTapped));
}

scan.dart

class ScanPage extends StatefulWidget {
      const ScanPage({super.key, required this.title});
    
      final String title;
    
      @override
      State<ScanPage> createState() => _ScanPageState();
    }
    
class _ScanPageState extends State<ScanPage> {
      late List<CameraDescription> cameras;
      late CameraController _controller;
      late Future<void> _initializeController;                                                
                                                                                                                      
      void startCamera() async {
        cameras = await availableCameras();
        _controller = CameraController(cameras[1], ResolutionPreset.high,
            enableAudio: false, imageFormatGroup: ImageFormatGroup.jpeg);
        _initializeController = _controller.initialize();
        await _initializeController;
      }
    
      @override
      void initState() {
        super.initState();
        startCamera();
      }
    
      @override
      void dispose() {
        _controller.dispose();

        super.dispose();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            extendBody: true,
            resizeToAvoidBottomInset: true,
            floatingActionButtonLocation: 
            FloatingActionButtonLocation.centerFloat,
            body: Stack(
              alignment: AlignmentDirectional.bottomEnd,
              children: [
                FutureBuilder<void>(
                  future: _initializeController,
                  builder: (context, snapshot) {
                    print(snapshot.connectionState == ConnectionState.done);
                    if (snapshot.connectionState == ConnectionState.done) {
                      return CameraPreview(_controller);
                    } else {
                      return const Center(child: CircularProgressIndicator());
                    }
                  },
                ),
              ],
            ));
      }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The _initializeController depends on other future method like availableCameras. Therefore there are some delay to initialize the _initializeController. You can use main startCamera as Future in this case.

  Future<void> startCamera() async {
    ......
  }

  late final future = startCamera();

And use

FutureBuilder<void>(
    future: future,
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading