The element type 'List<DropdownMenuItem<double>>' can't be assigned to the list type 'DropdownMenuItem<double>'

Viewed 211

I'm having a problem when ı try to automatize my Items which is a part of DropDownButton. Every time ı try to execute the function below ı get this error:The element type 'List<DropdownMenuItem>' can't be assigned to the list type 'DropdownMenuItem'.

I tried not to use map function and did the same as Flutter's official page that describes how to use a dropdown button but nothing has changed. I got the same error.

class DataHelper {
  static List<String> tumDerslerinHarflerii() {
    return ["AA", "BA", "BB", "CB", "CC", "CD", "DD", "FD", "FF"];
  }

  static double? harfiNotaCevir(String harf) {
    switch (harf) {
      case "AA":
        return 4;
      case "BA":
        return 3.5;
      case "BB":
        return 3.0;
      case "CB":
        return 2.5;
      case "CC":
        return 2;
      case "DC":
        return 1.5;
      case "DD":
        return 1.0;
      case "FD":
        return 0.5;
      case "FF":
        return 0.0;
    }
    return null;
  }

  static List<DropdownMenuItem<double>> tumDerslerinHarfleri() {
    return tumDerslerinHarflerii()
        .map(
          (e) => DropdownMenuItem(
            child: Text("$e"),
            value: harfiNotaCevir(e),
          ),
        )
        .toList();
  }
}

And I'm using it in my DropDownWidget:

_buildHarfler(GlobalKey buildHarfKey, double _value) {
    return Container(
      decoration: BoxDecoration(
        color: Sabitler.anaRenk.withOpacity(0.3),
        borderRadius: Sabitler.borderRadius,
      ),
      child: DropdownButton<double>(
        key: buildHarfKey,
        value: _value,
        items: [
          // Dropdown içindeki elementlerin her biri bir widget; liste istiyor.
          DataHelper.tumDerslerinHarfleri(),
        ],
        onChanged: (secilenDeger) {
          _value = secilenDeger!;
        },
      ),
    );
  }

2 Answers

change DataHelper.tumDerslerinHarfleri(), to ...DataHelper.tumDerslerinHarfleri(),

this will add the items in the list that are coming from your function (tumDerslerinHarfleri) to the items of your DropdownButton

You need to spread you list. [...DataHelper.tumDerslerinHarfleri()]

Related