Search code examples
fluttercamera

How to record videos and get its path with flutter?


I need to access the camera to record videos and get its path so that I can upload them to firebase storage, I tried Camera plugin, but threw this error The getter 'height' was called on null. , any ideas how ?

Code For Camera Plugin

initializeCamera() async {
    final cameras = await availableCameras();
    final firstCamera = cameras.first;
    cameraController = CameraController(firstCamera, ResolutionPreset.medium);
  }

  @override
  void initState() {
    initializeCamera();
    super.initState();
  }

return Scaffold(
      body: FutureBuilder(
        future: _future,
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            return AspectRatio(
              aspectRatio: cameraController.value.aspectRatio,
              child: CameraPreview(cameraController),
            );
          } else {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );

Solution

  • It was so simple, I just Had to initialize the camera before anything :).

    initializeCamera() async {
        await cameraController.initialize();
        final cameras = await availableCameras();
        final firstCamera = cameras.first;
        cameraController = CameraController(firstCamera, ResolutionPreset.medium);
      }