skip to Main Content

Scenario

I am trying to launch a camera stream screen in my Flutter app in order to capture a selfie using google_ml_kit_face_detection. For this purpose, I need to set a ResolutionPreset to my CameraController object. I am using the camera plugin for Flutter.

I am obtaining the phone’s RAM from system_info_plus which returns a Future<int?>.

In my initState() I am calling the function to obtain the RAM value, and then calling the function to initialize the CameraController object by passing the ResolutionPreset based on the phone’s RAM.

My Code

CameraController? _controller;
int? deviceMemory;

@override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) async {
      await getDeviceMemory();
      initCameraLens();
      await startLive();
    });
  }

Future<void> getDeviceMemory() async {
    deviceMemory = await SystemInfoPlus.physicalMemory; // returns in MB
  }

void initCameraLens() {
    // relevant code
  }

Future<void> startLive() async {
    final camera = cameras[_cameraIndex];

    // setup camera controller
    _controller = CameraController(
      camera,
      deviceMemory != null
          ? deviceMemory! <= 3072
              ? ResolutionPreset.low
              : ResolutionPreset.high
          : ResolutionPreset.high,
      enableAudio: false,
    );

    // start streaming using camera
    _controller?.initialize().then((_) {
      if (!mounted) {
        return;
      }

      // process camera image to get an instance of input image
      _controller?.startImageStream(_processCameraImage);

      setState(() {});
    });
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        fit: StackFit.expand,
        children: [
          _controller != null ?
            CameraPreview(_controller!) :
            Container(
              width: 100.w,
              height: 100.h,
              color: Colors.black,
            ),
         ],
       ),
    ),
  }

Issue Faced

When this screen launches, for a brief second or two I get the following error:
The following _CastError was thrown building CaptureFaceScreen(dirty, state: _CaptureFaceScreenState#38f97):
Null check operator used on a null value

after which the camera starts working and everything runs smoothly from thereon.

Request

I would like to know what’s causing this exception in my code and how to fix this issue.

2

Answers


  1. Chosen as BEST ANSWER

    I managed a workaround by initializing the the CameraController object with some static data before setting the desired values via the relevant functions in initState() calls.

    Instead of:

    CameraController? _controller;
    

    I am doing:

    CameraController _controller = CameraController(
        const CameraDescription(
          name: "",
          lensDirection: CameraLensDirection.front,
          sensorOrientation: 90,
        ),
        ResolutionPreset.high,
        enableAudio: false,
      );
    

  2. I think the getDeviceMemory() is not quick enough. So try to add a .then next to it instead, that way it wont continue until getDeviceMemory() is complete.

    @override
    void initState() {
      super.initState();
      getDeviceMemory().then((value) {
        initCameraLens();
        startLive();
      });
    }
    

    You shouldnt need to use WidgetsBinding.instance.addPostFrameCallback((_)

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