Home > OS >  is there anything wrong with passing the BuildContext context in methods and call them from outside
is there anything wrong with passing the BuildContext context in methods and call them from outside

Time:11-03

so, let's say I have this as example :

void exampleFunction(BuildContext context) {
 Navigator.of(context).pop();
}

from my UI, if I call that method :

 onPressed: () {
      exampleFunction(context);
    }

is this a normal or good practice or bad?

since the context in Flutter is a very important concept and sensitive, is it normal to do it that way in order to separate all logic into separate places / files

CodePudding user response:

This is surely opinionated so question should be closed. I'll just add a couple of things to consider when making up your own mind.

You should consider that the BuildContext is UI related and should perhaps not be associated with your business logic.

You will get loosely coupled widgets if several share the same "helper-methods" which might not be desirable.

  • Related