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(),
);
},
);
}
But I get this error
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
What goes wrong here?
To solve type 'List' is not a subtype of type 'List' Error you just need to assign a type to the map method. Here I am giving type to map<Widget> Or just converting Map to Widget will also solve your error. To solve type 'List' is not a subtype of type 'List' Error you just need to assign a type to the map method.
In dart and flutter, this example converts a list of dynamic types to a list of Strings. map() is used to iterate over a list of dynamic strings. To convert each element in the map to a String, toString() is used. Finally, use the toList() method to return a list.
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()
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With