简体   繁体   中英

Flutter exception: 'Context is not a subtype of BuildContext' error using Navigator

I am working on a flutter app and I am running into the following error: "The argument type 'Context' can't be assigned to the parameter type 'BuildContext'." However, when I try to pass in context to my functions as it says to do on the internet, I am unable to get it to work. Can someone please help me with this? The app is a camera app, and I know the video is successfully being recorded. here is the code:

This is the stop button that is pressed.

IconButton(
          icon: const Icon(Icons.stop),
          color: Colors.red,
          onPressed: () {controller != null &&
                  controller.value.isInitialized &&
                  controller.value.isRecordingVideo
              ? onStopButtonPressed. //this is the function that is being called
              : null;},
        ) //icons.stop

This is where the app isn't working with my Navigator.push call. It works fine when I take it out.

  void onStopButtonPressed() {
    stopVideoRecording().then((_) {
      if (mounted) setState(() {});
      print('Video recorded to: $videoPath');

      print('Navigator is hit');
      Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => PreviewImageScreen(videoPath: videoPath),
          ), //MaterialpageRoute
        );  //Navigator


    });


  }

And here is my stopVideoRecording function

Future<void> stopVideoRecording() async {
    if (!controller.value.isRecordingVideo) {
      return null;
    }

    try {
      await controller.stopVideoRecording();
    } on CameraException catch (e) {
      _showCameraException(e);
      return null;
    }

    //await _startVideoPlayer();


  }

Thanks!

change it to

Navigator.push(
      this.context, //add this so it uses the context of the class
      MaterialPageRoute(
        builder: (context) => PreviewImageScreen(videoPath: videoPath),
      ), //MaterialpageRoute
);  //Navigator

Maybe you're importing a library or something that has a class named context and is interfering with the name context

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM