繁体   English   中英

Flutter 异常:使用 Navigator 时出现“上下文不是 BuildContext 的子类型”错误

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

我正在开发一个 flutter 应用程序,我遇到了以下错误:“参数类型 'Context' 不能分配给参数类型 'BuildContext'。” 但是,当我尝试将上下文传递给我的函数时,正如它在互联网上所说的那样,我无法让它工作。 有人可以帮我吗? 该应用程序是一个相机应用程序,我知道视频已成功录制。 这是代码:

这是按下的停止按钮。

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

这是应用程序无法与我的 Navigator.push 通话一起使用的地方。 当我把它拿出来时它工作得很好。

  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


    });


  }

这是我的 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();


  }

谢谢!

将其更改为

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

也许您正在导入一个库或具有 class 命名上下文并干扰名称上下文的东西

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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