Home > Back-end >  Flutter get context value from outside context
Flutter get context value from outside context

Time:09-17

I am very new to Flutter so I'm not sure if my train of thoughts even make sense. I'm currently using a package called EasyLocalization to localize my app. In doing so, I am trying to reference the current locale of my application which is set by EasyLocalization in a service class completely independent of widgets but the only methods offered by the package was to reference the context

In their example, the following code works

print(context.locale.toString());

However, since I want the value in a "service" class that doesn't make use of widgets, I'm not able to call on any context at all. So something like this code works on my widget but not in an independent service class since context doesn't exist there

var currentLocale = EasyLocalization.of(context)?.locale ?? 'en';

I've also tried some other code to get the localization but they turn out different from the actual localization my app is sync'ed to. For example when my app is running off 'zh' in EasyLocalization, other methods such as the ones below only return 'en-US'

print(Intl().locale);
print(Intl.getCurrentLocale());

One way I've gotten it to partially work is to create a function inside my widget that sets a global value when clicked and then reference that value but it feels "hacky" and isn't sufficient for my use-case where data gets loaded on application start which is then passed through a translation function using the context locale. Most other search results also only turn up information for navigation and snackbars which don't seem to help my use-case so I'm currently out of ideas and turning to SO for help.

Below is my MaterialApp() if it helps

Widget build(BuildContext context) {
    return ProviderScope(
        child: MaterialApp(
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      debugShowCheckedModeBanner: false,
      // Todo: Implement dark mode color theme
      theme: lightTheme,
      onGenerateRoute: AppRouter.generateRoutes,
    ));
  }

CodePudding user response:

You can use navigator key to access current context from anywhere. You have to create global key and pass it to material app.

//create key
final navigatorKey = new GlobalKey<NavigatorState>();

//pass it to material app
Widget build(BuildContext context) {
    return ProviderScope(
        child: MaterialApp(
      navigatorKey: navigatorKey,  //key
      localizationsDelegates: context.localizationDelegates,
      supportedLocales: context.supportedLocales,
      locale: context.locale,
      debugShowCheckedModeBanner: false,
      // Todo: Implement dark mode color theme
      theme: lightTheme,
      onGenerateRoute: AppRouter.generateRoutes,
    ));
  }

//access context
print(navigatorKey.currentContext.locale.toString());

CodePudding user response:

Your service class should be locale independent by design, so taking locale as an input, for example:

class I18nService {
  final String locale;
  
  I18nService(this.locale);
  
  String sayHello() {
    if (locale == 'en_CA') {
      return 'hello Canada';
    }
    return 'hello world';
  }
}

I'd also recommend looking into the provider package, you can instantiate your service using a ProxyProvider and pass in the locale there.

  • Related