Home > OS >  Error: Required named parameter 'path' must be provided
Error: Required named parameter 'path' must be provided

Time:02-25

im getting below errors in my code. how to solve this. appreciate your help on this

Error: Required named parameter 'path' must be provided.
Navigator.push(context, MaterialPageRoute(builder: (builder)=> CameraViewPage()));

Context: Found this candidate, but the arguments don't match. const CameraViewPage({Key? key, required this.path}) : super(key: key);

Error: Too many positional arguments: 0 allowed, but 1 found. Try removing the extra positional arguments. await _cameraController.takePicture(path);

CameraSreen.dart

  void takePhoto(BuildContext context) async {
   final path = join((await getTemporaryDirectory()).path,"${DateTime.now()}.png");
   await _cameraController.takePicture(path);
   Navigator.push(context, MaterialPageRoute(builder: (builder)=> CameraViewPage()));
  }

CameraView.dart

class CameraViewPage extends StatelessWidget {
  const CameraViewPage({Key? key, required this.path}) : super(key: key);
  final String path;

CodePudding user response:

As you can see:

CameraViewPage({Key? key, required this.path}) : super(key: key);

The argument path is required.

So this is how you should call Navigator.push:

Navigator.push(context, MaterialPageRoute(builder: (builder)=> CameraViewPage(path: path)));

If you read the error message about takePicture, it says you shouldn't pass any POSITIONAL parameter. Without any more details, I assume takePicture takes a NAMED parameter path. Try:

await _cameraController.takePicture(path: path);

CodePudding user response:

Try this you have made path as required argument for CameraViewPage class so you are not passing path while navigating to next screen.

  void takePhoto(BuildContext context) async {
       final path = join((await getTemporaryDirectory()).path,"${DateTime.now()}.png");
       await _cameraController.takePicture(path);
       Navigator.push(context, MaterialPageRoute(builder: (builder)=> CameraViewPage(path:path)));
      }
  • Related