'I Can't get flutter camera plugin to work from a second screen

I have been trying to get the flutter camera plugin to work for almost a week now but I couldn't. Every time the camera is not initialized no matter what workaround I do.

Can any one help please.

class CameraPage extends StatefulWidget {
      const CameraPage({Key? key,}) : super(key: key); @override
      State<CameraPage> createState() => _CameraPageState();
    }
    
    class _CameraPageState extends State<CameraPage> {
      List<CameraDescription>? cameras;
      CameraDescription? camera;
      CameraController? cameraController;
      CameraDescription? firstCamera() {
        camera = cameras![1];
        return camera;
      }
    
      Future getAvailableCameras() async {
        cameras = await availableCameras();
        camera = firstCamera();
      }
    
      Future setDefaultCamera() async {
        cameraController = CameraController(
            Provider.of<CameraProvider>(context).camer!, ResolutionPreset.medium);
        cameraController?.initialize();
        return cameraController;
      }
    

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: FutureBuilder(
              future: setDefaultCamera(),
              builder: ((context, snapshot) {
                if (snapshot.hasData) {
                  return Container(
                      width: 450,
                      color: Colors.red,
                      height: 450,
                      child: CameraPreview(cameraController!));
                } else {
                  return const Center(child: CircularProgressIndicator());}})),);}}


Solution 1:[1]

To anyone facing the same problem and want more control than using image_picker. I found a solution to my problem by following the example in the this showcase. https://dev.to/aouahib/build-a-flutter-gallery-to-display-all-the-photos-and-videos-in-your-phone-pb6 I hope someone will find it useful.

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Mutken