I am trying to get the Locale of the user's phone at app start.
I have this Widget tree in the runApp() method :
@override
Widget build(BuildContext context) {
return MaterialApp(
locale: Locale(Localizations.localeOf(context).languageCode), // This crashes
localizationsDelegates: [
const LocalizationDelegate(), // My custom delegate to get translations
CountryLocalizations.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: [
Locale("en"),
Locale("fr"),
],
debugShowCheckedModeBanner: false,
home: Scaffold(
resizeToAvoidBottomInset: false,
body: HomePage(),
)
);
}
the line locale: Locale(Localizations.localeOf(context).languageCode) cause a crash with :
Requested the Locale of a context that does not include a Localizations ancestor.
I simply want to bind this locale to my Delegate without going further in app.
So far, i put locale: Locale("en") to read my en.json file from my LocalizationDelegate.