Member-only story
Power Of Riverpod, Localizations Without BuildContext
4 min readMar 19, 2023
There are times in the Flutter App when we need to access localizations without the BuildContext such is in app error messages to the user. Let me show you the dart trick to access providers without the Flutter framework BuildContext.
The Dart Method
The dart trick is to use the WidgetsBindingObserver to cause the state to re-build upon the locale change:
Then the full code, including the above _LocaleObserver:
So in the provider, we are doing this:
1. Initialize from the initial locale
2. Create an observer to update the state
3. Register the observer and dispose it when no longer needed