'Take Multiple Picture Using Flutter Camera Plugin

I'm trying to take multiple photos using the camera plugin, am doing that inside a loop and I keep getting the error below:

CameraException (CameraException(Previous capture has not returned yet., takePicture was called before the previous capture returned.))

At the moment I don't know how to wait for the previous capture to return before I take the next picture inside the loop.

This is the code I have currently:

 for (var i = 0; i < numOfPhotos; i++) {
            print('immediate capture begins');
            await takePicture();
    }

    Future<void> takePicture() async {
     try {
      var imageFile = await cameraController.value.takePicture();
      imageFiles.add(imageFile); //imageFiles is declared somewhere
    } on CameraException catch (e) {
      print('camera exception: ' + e.description!);
    }
  }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source