我有这个代码:
List<Widget> _elementsHere = _locationsRegister
.map((e) => ShowingLocation(
num: e.num,
name: e.name,
icon: e.icon,
))
.toList();
Run Code Online (Sandbox Code Playgroud)
我已经指定了List<Widget>,但是如果我_elementsHere.runtimeType.toString()在控制台中打印,我可以看到List<ShowingLocation>. 事实上,如果我添加此代码:
_elementsHere.insert(0, Text('hello'));
Run Code Online (Sandbox Code Playgroud)
尽管它是一个小部件,但我收到的错误Text不是 的子类型ShowingLocation。
我想要_elementsHereasList<Widget>而不是List<ShowingLocation>.
这可以通过在map函数的泛型字段中指定对象类型来解决。由于您没有指定类型,因此假定它是ShowingLocation.
改为这样做:
List<Widget> _elementsHere = _locationsRegister
.map<Widget>((e) => ShowingLocation(
num: e.num,
name: e.name,
icon: e.icon,
))
.toList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |