Search code examples
flutterlocalization

The getter 'languageCode' isn't defined for the type 'List<Locale>'


i need my application to support different languages. But i have a problem using languageCode attribute. the code is:

  supportedLocales: [
        Locale('en', ''),
        Locale('ar', ''),
      ],
      localeListResolutionCallback: (currentLocale, supportedLocales) {
        if (currentLocale != null) {
          print(currentLocale.languageCode);
          for(Locale locale in supportedLocales){
           // if(currentLocale.languageCode== )
          }
        }
        return supportedLocales.first;
      },

the problem is : The getter 'languageCode' isn't defined for the type 'List'. Try importing the library that defines 'languageCode', correcting the name to the name of an existing getter, or defining a getter or field named 'languageCode'.


Solution

  • In your code currentLocale is an array. You should define it locales for more correct name. You need to use it like this:

        localeListResolutionCallback: (locales, supported) {
              locales.forEach((l) => print(l.languageCode));