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>类型(需要存储多个用户信息)
如果需要往列表里添加单个用户对象,有两种写法:
- 使用
add方法(推荐,更直观):
eventsWithoutUserData[i].userInformation.add(userInformationSnapList[j]);
- 把单个对象包装成列表后用
addAll:
eventsWithoutUserData[i].userInformation.addAll([userInformationSnapList[j]]);
内容的提问来源于stack exchange,提问作者Peter
相关产品推荐
相关产品推荐

