首页 > 解决方案 > 'List 类型的值' 不能分配给类型为 'Iterable 的变量'

问题描述

我不知道这是否发生在我切换到 Flutter 的 beta 通道之前,但我不明白为什么这是一个错误。列表是可迭代的,对吗?我从官方文档中获取了示例。

Iterable<int> example() {
  Iterable<int> iterable = [1, 2, 3];
  return iterable;
}

VSCode 用红色下划线标记列表,告诉我:

A value of type 'List<int>' can't be assigned to a variable of type 'Iterable<int>'.
Try changing the type of the variable, or casting the right-hand type to 'Iterable<int>'. dart(invalid_assignment)

标签: flutterdart

解决方案


我不知道是什么问题,但我尝试从 VSCode 中删除 Flutter 和 Dart 扩展,重新启动整个计算机并再次安装它们(按此顺序),现在相同的代码可以工作了!

正如BeefPapa在评论中所说,代码是绝对正确的。

谁知道发生了什么。


推荐阅读