【问题标题】:How to solve "The argument type 'List<String>' can't be assigned to the parameter type 'String"如何解决“无法将参数类型'List<String>'分配给参数类型'String”
【发布时间】:2022-01-18 10:09:16
【问题描述】:

这是我的代码

final _months = [
    ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
  ];

......

DropdownButtonFormField(
              hint: const Text('Select Period'),
              items: _months.map((e) {
                return DropdownMenuItem(
                  value: e,                                                      
                  child: Text(e //here is the error showing),
                );
              }).toList(),
              onChanged: (value) {
                //do something
              },
            ),

如何解决这个问题,我只是将月份映射值从 dfault 更改为动态,然后错误就消失了,但是当我运行应用程序时,它显示另一个错误,例如“这不是字符串的子类型” 如何解决这个问题以及为什么会发生这种情况

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:

    您有一组额外的[ ],因此您的_months 变量的类型为List&lt;List&lt;String&gt;&gt;

      final _months = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    

    【讨论】:

      【解决方案2】:

      你有数组里面的数组:正确的是:

      final _months = [
          "January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
        ];
      

      【讨论】:

      • 我已经解决了,但是谢谢
      猜你喜欢
      • 2021-08-26
      • 2021-10-06
      • 2020-08-26
      • 2022-12-12
      • 2022-11-17
      • 2021-09-13
      • 2021-10-02
      • 2021-10-18
      • 2019-09-23
      相关资源
      最近更新 更多