输入'列表<动态>'不是“List<Widget>"类型的子类型 [英] type 'List<dynamic>' is not a subtype of type 'List<Widget>'
本文介绍了输入'列表<动态>'不是“List<Widget>"类型的子类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一段从 Firestore 示例中复制的代码:
I have a snippet of code which I copied from Firestore example:
Widget _buildBody(BuildContext context) {
return new StreamBuilder(
stream: _getEventStream(),
builder: (context, snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text("Class"),
);
}).toList(),
);
},
);
}
但我收到此错误
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
这里出了什么问题?
推荐答案
这里的问题是类型推断以意想不到的方式失败.解决方案是为 map
方法提供一个类型参数.
The problem here is that type inference fails in an unexpected way. The solution is to provide a type argument to the map
method.
snapshot.data.documents.map<Widget>((document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text("Class"),
);
}).toList()
更复杂的答案是,虽然 children
的类型是 List
,但该信息不会返回到 map
> 调用.这可能是因为 map
后面跟着 toList
并且因为没有办法对闭包的返回进行类型注释.
The more complicated answer is that while the type of children
is List<Widget>
, that information doesn't flow back towards the map
invocation. This might be because map
is followed by toList
and because there is no way to type annotate the return of a closure.
这篇关于输入'列表<动态>'不是“List<Widget>"类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文