admin管理员组

文章数量:1122846

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: Imagework('/250?image=9'));
  }).toList(),

Is anyone able to assist?

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?

Share Improve this question edited Nov 21, 2024 at 10:50 DarkBee 15.8k8 gold badges69 silver badges110 bronze badges asked Nov 21, 2024 at 10:45 user1052610user1052610 4,66914 gold badges60 silver badges108 bronze badges 4
  • Did you try and dump what CountryFlag.fromLanguageCode(...) actually returns? – DarkBee Commented Nov 21, 2024 at 10:50
  • From the source code it appears to return a SizedBox which contains an Image – user1052610 Commented Nov 21, 2024 at 11:04
  • Well seems you are passing a SizedBox to a property which expect an Image, e.g. labelWidget: Image.network('https://picsum.photos/250?image=9')) – DarkBee Commented Nov 21, 2024 at 11:05
  • As labelWidget expects a Widget, why would it not accept a SizedBox? – user1052610 Commented Nov 21, 2024 at 11:17
Add a comment  | 

2 Answers 2

Reset to default 1

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.

You can solve it changing this line inside file flag_code:

.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 name of country. The package is using entry.key but those values are like "hi" to "in" or "pt-br" to "br"... So if you really want to use "in" or "br" do the change in code.

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

本文标签: flutterDisplaying a CountryFlag in a dropdown listStack Overflow