未定义的名称'上下文'在fuction中
我在任何单独的DART文件中定义了shownackbarr()函数,并在其他DART文件中使用,但在此凝视**上下文下显示了红线。
shownackbar(**上下文**,e.tostring);
showSnackBar(BuildContext context, String text) {
return ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(text),
));
}
on FirebaseAuthException catch(e){
showSnackBar(context, e.message!);
res = false;
}
I defined a showSnackBarr() function in any separate dart file and used in other dart file but showing red line under this stared ** context.
showSnackBar( **context** , e.toString);
showSnackBar(BuildContext context, String text) {
return ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(text),
));
}
on FirebaseAuthException catch(e){
showSnackBar(context, e.message!);
res = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
没有变量/实例成员称为
上下文
您在其中调用函数。通常,上下文
可作为属性状态
对象。buildContext
的实例也传递到build
小部件的方法中。有关
buildcontext
的更多信息。There is no variable/instance member called
context
where you're invoking your function. Generally,context
is available as a property inState
objects. An instance ofBuildContext
is also passed into thebuild
method of Widgets.More on
BuildContext
.似乎您正在尝试从小部件外部的功能显示Snackbar。
为此,您必须将buildContext与任何参数一起传递到该函数。
示例:
并从小部件调用函数
,或者
您可以使用全局上下文如果您不想通过构建上下文每次。
Seems like you are trying to show the snackbar from the function outside of the widget.
For that, you have to pass the BuildContext to the function along with any parameters.
Example:
and call the function from the widget as
OR
you can use global context if you do not want to pass the build context each time.
我这样做了,我将一个变量注入了类中,其中使用此功能它正在运行最终
buildContext上下文; authmethods({需要this.context});
并制作构造函数。
I did this that I intialize a variable into the class where I using this function it is working final
BuildContext context ; AuthMethods({required this.context });
And make a constructor.