Search code examples
fluttercameraflutter-dependencies

Not able to use camera resource after updating camera: ^0.10.0 takepicture() function does not working


I'm not able to use cameraControllers.takePicture after updating the CAMERA version from 0.5.8+17 to 0.10.0 in pubspec.yaml

I've attached last code. Please correct me where I'm wrong

try {
  final p = await getTemporaryDirectory();
  final name = DateTime.now();
  final path = "${p.path}/$name.png";


  await cameraController.takePicture(path).then((value) {
    if (valuetype == '1') {
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
              builder: (context) => AddProfile(
                    imgPath: path,
                    fileName: "$name.png",
                    imagetype: "1",
                  )));
    } else {
      Navigator.push(
          context,
          MaterialPageRoute(
              builder: (context) => MyApp(
                    imgPath: path,
                    fileName: "$name.png",
                  ))).then((erg) {
        if (Navigator.canPop(context)) {
          Navigator.pop(context);
        } else {
          SystemNavigator.pop();
        }
      });
    }
    print(imgPath + 'image path');
  });



} catch (e) {
  showCameraException(e);
}

}


Solution

  •   onCapture(context) async {
        try {
          File imageFile;
          // final p = await getTemporaryDirectory();
          XFile xFile = await cameraController.takePicture();
          if (xFile != null) {
            imageFile = File(xFile.path);
          }
          final name = DateTime.now();
          // final path = "${imageFile.path}/$name.png";
    
          if (imageFile != null && imageFile.existsSync()) {
            if (valuetype == '1') {
                  Navigator.pushReplacement(
                      context,
                      MaterialPageRoute(
                          builder: (context) => AddProfile(
                                imgPath: imageFile.path,
                                fileName: "$name.png",
                                imagetype: "1",
                              )));
                } else {
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => MyApp(
                                imgPath: imageFile.path,
                                fileName: "$name.png",
                              ))).then((erg) {
                    if (Navigator.canPop(context)) {
                      Navigator.pop(context);
                    } else {
                      SystemNavigator.pop();
                    }
                  });
                }
                print(imgPath + 'image path');
          } else {
            print('Image file does not exist');
          }
        } catch (e) {
          showCameraException(e);
        }
      }
    

    this is worked for me