You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中如何在列表值中存另一个列表?遇类型不匹配错误

问题解决:type 'UserForPosts' is not a subtype of type 'Iterable'

错误原因

报错的核心是**addAll方法的参数类型不匹配**:addAll要求传入可迭代对象(比如List、Set这类集合),但你代码里直接传了单个UserForPosts实例,导致类型不兼容。

解决方案

根据eventsWithoutUserData[i].userInformation的实际类型,选择对应处理方式:

情况1:userInformation是单个UserForPosts对象(不是列表)

如果你的业务逻辑是给每个事件绑定对应的单个用户信息,直接用赋值操作替代addAll:

eventsWithoutUserData[i].userInformation = userInformationSnapList[j];

情况2:userInformation是List<UserForPosts>类型(需要存储多个用户信息)

如果需要往列表里添加单个用户对象,有两种写法:

  1. 使用add方法(推荐,更直观):
eventsWithoutUserData[i].userInformation.add(userInformationSnapList[j]);
  1. 把单个对象包装成列表后用addAll:
eventsWithoutUserData[i].userInformation.addAll([userInformationSnapList[j]]);

内容的提问来源于stack exchange,提问作者Peter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 17:20:34