Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter app error - getter 'value' was called on null

Tags:

flutter

dart

I'm building one app where on top of Camera view I need to show something. Here is my code.

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';

List<CameraDescription> cameras;

void main() async {
  runApp(new MaterialApp(
    home: new CameraApp(),
  ));

}

class CameraApp extends StatefulWidget {
  @override
  _CameraAppState createState() => new _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  CameraController controller;
  var cameras;
  bool cameraGot = false;

  Future<Null> getCamera() async {
    cameras = await availableCameras();
    setState(() {
      this.cameras = cameras;
      this.cameraGot = true;
    });
  }

  @override
  void initState() {
    getCamera();
    super.initState();

    if(this.cameraGot) {
      controller = new CameraController(this.cameras[0], ResolutionPreset.medium);
      controller.initialize().then((_) {
        if (!mounted) {
          return;
        }
        setState(() {});
      });
    }

  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    // camera widget
    Widget cameraView = new Container(
      child: new Row(children: [
          new Expanded(
            child: new Column(
                children: <Widget>[
                  new AspectRatio(
                    aspectRatio:
                        controller.value.aspectRatio,
                        child: new CameraPreview(controller)
                  )
                ]
            ),
          )
      ])
    );


    return new Scaffold(
      body: new Stack(
        children: <Widget>[
          !this.controller.value.initialized ? new Container() : cameraView,

           // ---On top of Camera view add one mroe widget---

        ],
      ),
    );
  }
}

Whenever I'm building the app I'm getting following error...

I/flutter ( 6911): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 6911): The following NoSuchMethodError was thrown building CameraApp(dirty, state: _CameraAppState#b6034):
I/flutter ( 6911): The getter 'value' was called on null.
I/flutter ( 6911): Receiver: null
I/flutter ( 6911): Tried calling: value

Can't able to fig. out what's the error.

like image 662
Suresh Avatar asked May 12 '18 08:05

Suresh


1 Answers

If you have created and assigned value to the variable and still it shows getter 'value' was called on null, try to Run or Restart your app instead of Hot Reload. Because Hot Reload will not call initstate() (where variables assign their values) which will be only called by Restarting the app.

like image 123
Mohan Munisifreddy Avatar answered Oct 03 '22 03:10

Mohan Munisifreddy