参数类型“对象?”不能分配给参数类型 List

Posted

技术标签:

【中文标题】参数类型“对象?”不能分配给参数类型 List【英文标题】:The argument type 'Object?' can't be assigned to the parameter type List 【发布时间】:2021-11-06 18:49:37 【问题描述】:

我一直在尝试将我的应用程序转换为新的 Flutter 新版本,但出现此错误 The argument type Object? can't be assigned to the parameter type List that can't be fix.... 可以有人帮我解决这个问题

list() 
    return Expanded(
      child: FutureBuilder(
        future: employees,
        builder: (context, snapshot) 
          if (snapshot.hasData) 
            return dataTable(List<Url>.from(snapshot.data));
          

          if (null == snapshot.data || snapshot.data == 0) 
            return Text("Tiada Data");
          

          return CircularProgressIndicator();
        ,
      ),
    );
  

【问题讨论】:

【参考方案1】:

这是因为您需要转换FutureBuilder 的类型。根据您的代码,我推断employees 的类型为Future&lt;List&gt; 或至少Future&lt;Iterable&gt;,那么您应该像这样定义构建器的类型:

FutureBuilder<Iterable>(
  future: employees,
  builder: (context, snapshot) 
    if (snapshot.hasData) 
      return dataTable(List<Url>.from(snapshot.data!));
    

    // ...
  ,
),

【讨论】:

参数类型 'List?'无法分配给参数类型“Iterable”我收到此错误 你的可变员工类型是什么? 您可能只需要强制转换为不可为空的 snapshot.data,如下所示:List&lt;Url&gt;.from(snapshot.data!) 未来>员工;

以上是关于参数类型“对象?”不能分配给参数类型 List的主要内容,如果未能解决你的问题,请参考以下文章