未定义的名称'上下文'在fuction中

发布于 2025-02-01 15:44:25 字数 421 浏览 3 评论 0原文

我在任何单独的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小清晰的声音 2025-02-08 15:44:25

没有变量/实例成员称为上下文您在其中调用函数。通常,上下文可作为属性 状态 对象。 buildContext的实例也传递到build小部件的方法中。

有关 buildcontext 的更多信息。

There is no variable/instance member called context where you're invoking your function. Generally, context is available as a property in State objects. An instance of BuildContext is also passed into the build method of Widgets.

More on BuildContext.

掩耳倾听 2025-02-08 15:44:25

似乎您正在尝试从小部件外部的功能显示Snackbar。

为此,您必须将buildContext与任何参数一起传递到该函数。

示例:

void myFunc(BuildContext context, dynamic data){
    try{
        // perform operation
    }
    catch(e){
        showSnackBar(context, e.message!);
    }
}

并从小部件调用函数

myFunc(context, "any data");

,或者

您可以使用全局上下文如果您不想通过构建上下文每次。

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:

void myFunc(BuildContext context, dynamic data){
    try{
        // perform operation
    }
    catch(e){
        showSnackBar(context, e.message!);
    }
}

and call the function from the widget as

myFunc(context, "any data");

OR

you can use global context if you do not want to pass the build context each time.

絕版丫頭 2025-02-08 15:44:25

我这样做了,我将一个变量注入了类中,其中使用此功能它正在运行最终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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文