简体   繁体   English

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

[英]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'."我正在开发一个 flutter 应用程序,我遇到了以下错误:“参数类型 'Context' 不能分配给参数类型 '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.这是应用程序无法与我的 Navigator.push 通话一起使用的地方。 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这是我的 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也许您正在导入一个库或具有 class 命名上下文并干扰名称上下文的东西

暂无
暂无

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

相关问题 Flutter 错误:68:14:错误:找不到 Getter:“上下文”。 和未处理的异常:“空”类型不是“BuildContext”类型的子类型 - Flutter Error :68:14: Error: Getter not found: 'context'. and Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext' 颤振:异常使用 Navigator.of(context).pop(); - Flutter: Exception using Navigator.of(context).pop(); Flutter [错误:flutter/lib/ui/ui_dart_state.cc(209)] 未处理的异常:“Null”类型不是“BuildContext”类型的子类型 - Flutter [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext' “Context”类型不是“BuildContext”类型的子类型 - type 'Context' is not a subtype of type 'BuildContext' &#39;(BuildContext, Exception) =&gt; void&#39; 不是类型 &#39;(BuildContext, Exception) =&gt; () =&gt; void&#39; 的子类型 - '(BuildContext, Exception) => void' is not a subtype of type '(BuildContext, Exception) => () => void' Flutter:将Navigator.of(context)用于RaisedButton的onPressed事件时发生异常 - Flutter : Exception on using Navigator.of(context) for onPressed event for RaisedButton 导航器错误在颤振导航器错误参数类型&#39;JsObject&#39;不能分配给参数类型&#39;BuildContext&#39; - navigator error in flutter navigator error The argument type 'JsObject' can't be assigned to the parameter type 'BuildContext' Flutter 导航器推送替换中的上下文错误 - Flutter Context Error in Navigator pushReplacement Flutter Navigator“无法将参数类型&#39;Context&#39;分配给参数类型&#39;BuildContext&#39;” - Flutter Navigator “argument type 'Context' can't be assigned to the parameter type 'BuildContext'” 未处理的异常:“Null”类型不是“BuildContext”类型的子类型 - Unhandled Exception: type 'Null' is not a subtype of type 'BuildContext'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM