Home > Blockchain >  Is there a way of including internationalized strings outside of the build function in flutter?
Is there a way of including internationalized strings outside of the build function in flutter?

Time:12-23

Is there a way to internationalize strings in flutter outside of the Widget build function? Normally, I would use this:

AppLocalizations.of(context)!.settings

to access internationalized strings I had defined in the app_{language}.arb file but this requires the context parameter which is not available outside the build function. I have tried googling the issue but I can't find any solutions. Do the strings have to be defined inside of the build function?

CodePudding user response:

You can define a navigator key globally and access its context anywhere across app.

Define this globally in main file.

final navigatorKey = GlobalKey<NavigatorState>();

pass this navigatorKey in Material App navigator key

MaterialApp(
    navigatorKey: navigatorKey,
    ...
),

Now you can access context from anywhere in app using

BuildContext context = navigatorKey.currentState!.context;
AppLocalizations.of(context)!.loremIpsum
  • Related