[英]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.