I'm trying to use the intl plugin to get more translations of my app, but it always needs a context. In files without context where I simply have to assign a value to the variable what should I do?
For example I would like to do something like this::
String yes = "${AppLocalizations.of(context)!.yes}"
As other answers suggest you can put your variable
inside build
, however, if you don't want to do that then you can define a Navigator Key
and get the current context
from it anywhere in your code.
Define Navigator Key:
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
Attach it to your MaterialApp
:
MaterialApp(
navigatorKey: navigatorKey,
...
),
Get current context from navigatorKey:
String yes = "${AppLocalizations.of(navigatorKey.currentContext!)!.yes}