【问题标题】:The argument type 'List<String>' can't be assigned to the parameter type 'List<String>Function()'参数类型“List<String>”不能分配给参数类型“List<String>Function()”
【发布时间】:2025-12-04 04:40:01
【问题描述】:

我有一个由 String 键和 List 值组成的 Map。但是,当我尝试向 Map 添加新值时,出现错误“无法将参数类型 'List' 分配给参数类型 'ListFunction()'”。

示例代码如下:

Map<String, List<String>> map = new Map<String, List<String>>();
String key = "Key1";
List<String> currentValues = [];

map.putIfAbsent(key,currentValues);

最后一行抛出上面的错误。有没有人解决这个问题?

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    the docs 开始,putIfAbsent 将函数作为第二个参数。所以你应该写这样的东西:

    map.putIfAbsent(key, () => currentValues);
    

    【讨论】:

      最近更新 更多