Is it possible to listen when the user changes their system language? I need to clear some list when the user changes their language. Now I am detecting the language in the void main()
function on startup.
void main() async {
// Firebase Push Notifications initialization
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(backgroundHandler);
detectLanguageChange();
WidgetsBinding.instance.addObserver(this);
runApp(const MyApp());
}
class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
@override
void didChangeLocales(List<Locale>? locales) {
// TODO: implement didChangeLocales
super.didChangeLocales(locales);
print("Locales changed");
}
It needs to run 'detectLanguage' every time the language changes.