元素类型; list< dropdownmenuitem< double>>>'可以将列表类型分配给列表类型。

发布于 2025-01-19 18:28:26 字数 1711 浏览 0 评论 0原文

当我尝试自动化作为 DropDownButton 一部分的项目时,我遇到了问题。 每次我尝试执行下面的函数时,我都会收到此错误:元素类型“List”无法分配给列表类型“DropdownMenuItem”。

我尝试不使用地图功能,并按照 Flutter 官方页面描述如何使用下拉按钮的方式进行操作,但没有任何改变。我遇到了同样的错误。

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();
  }
}

我在 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!;
        },
      ),
    );
  }

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!;
        },
      ),
    );
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沫尐诺 2025-01-26 18:28:26

DataHelper.tumDerslerinHarfleri(), 更改为 ...DataHelper.tumDerslerinHarfleri(),

这将在列表中添加来自您的函数的项目 (tumDerslerinHarfleri) 到 DropdownButtonitems

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

去了角落 2025-01-26 18:28:26

你需要传播你的清单。 [...DataHelper.tumDerslerinHarfleri()]

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文