Search code examples
flutterdartflutter-widget

Undefined name 'context' in fuction


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;
}

Solution

  • 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.