0

I have some methods that require the buildContext, e.g. popping the current page, showing modals, hiding the snackBar.

I'm not sure which would be the better solution for the methods to obtain the buildContext:

  1. Creating a global key of the scaffold state and calling the global key's currentContext,
  2. Passing the buildContext from the build method all the way down to the current method

Is there a difference between the two ways? Or if they're equally suitable in the situation, is one way preferred over or is of better convention than the other?

Thanks

1 Answers1

0

I think that using global keys is considered bad practice in most cases. And I think that functions having a BuildContext as a parameter is common place when using Navigator, Theme and MediaQuery. Keep in mind though that it's better practice to use classes over functions to make reusable widget-tree.

MickaelHrndz
  • 3,604
  • 1
  • 13
  • 23