Click here to Skip to main content
15,667,014 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everyone,

I would like to retrieve from my flutter/dart project, the current system lqnguage code, like. fr, en, ...

What I have tried:

I have tried the following:

Locale myLocale = Localizations.localeOf(context);
String lc = currentLanguageCode(myLocale);

print ("Language code: ${lc}");

The problen is from the void initState() statement, the context is null and will then return an empty string.

I was hoping to have a method like:

Does anyonce have an idea how to grab this language code from everywhere in the project.

Thank you very much in advance.
Best regards.
Updated 3-Feb-21 0:36am
Kornfeld Eliyahu Peter 5-May-19 5:39am    
Maybe this one:

1 solution

use didChangeDependencies method,the method is called after the initState is called and it gives you access to the context.

Your code will look like this
  void didChangeDependencies() {
    Locale myLocale = Localizations.localeOf(context);
    print('my locale ${myLocale}');
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900