skip to Main Content

I want to display CountryFlags, defined in package:country_flags/country_flags.dart, in a flutter DropdownMenu. However, this sample code below only displays a ? :

  dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) {
    return DropdownMenuEntry<String>(
        value: value,
        label: value,
        labelWidget: CountryFlag.fromLanguageCode(
          'in',
          width: 20,
          height: 20,
        ));
  }).toList(),

The same code modified to display an image, however, works fine:

  dropdownMenuEntries: list.map<DropdownMenuEntry<String>>((String value) {
    return DropdownMenuEntry<String>(
        value: value,
        label: value,
        labelWidget: Image.network('https://picsum.photos/250?image=9'));
  }).toList(),

Is anyone able to assist?

2

Answers


  1. You can solve it changing this line:

    .singleWhereOrNull((entry) => entry.key== languageCode)
    

    to

    .singleWhereOrNull((entry) => entry.value == languageCode)
    

    You can find it inside the flag_code.dart and in

    static String? fromCountryCode(String countryCode)
    

    The mistake here looks like that are in the package. They are using entry.key but those value are like "hi" to "in" or "pt-br" to "br"…

    Or you can simply pass the right "key" to the package so, wont be necessary change anythinhg.

    Login or Signup to reply.
  2. The ? symbol indicates that the flag for the provided language code (in) does not exist. This happens because in is not a valid language code. If you’re looking for the language code for India, it is hi.

    As stated in the package documentation:

    For a list of supported languages, visit Lingoes Language Codes. (Please note that not all language codes on the list are supported.)

    Make sure the language code you use is supported by the country_flags package. If it still doesn’t work, double-check the package documentation for limitations or updates regarding supported language codes.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search