Hello Guys, How are you all ? Hope you all are fine. Today I have faced type ‘List’ is not a subtype of type ‘List’ this error where I Use in my Flutter Firebase Project.
How type ‘List’ is not a subtype of type ‘List’ in Flutter error occur?
I have Used Firestore Example in My Flutter Project. When I run my flutter app. But I get this error in my Console.
I am using This Below Code Where I Got Error.
children: snapshot.data.documents.map((document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text("Class"),
);
}).toList()
I get this below error
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
Here I am Come with all Possible Solution. So let’s Start this tutorial without wasting your Time. Let’s start this article.
How to solve type ‘List’ is not a subtype of type ‘List’ in Flutter error?
- How to solve type 'List
' is not a subtype of type 'List ' in flutter? to solve type 'List<dynamic>' is not a subtype of type 'List<Widget>' in flutter Problem is that type inference fails in an unexpected way. The solution is to provide a type argument to the map method. Because of map is followed by toList and because there is no way to type annotate the return of a closure. This Solution Worked for me. hope it will also work for you.
- type 'List
' is not a subtype of type 'List ' to solve type 'List<dynamic>' is not a subtype of type 'List<Widget>' in flutter Problem is that type inference fails in an unexpected way. The solution is to provide a type argument to the map method. Because of map is followed by toList and because there is no way to type annotate the return of a closure. This Solution Worked for me. hope it will also work for you.
Solution 1 : Provide type argument to the map
method.
Problem is that type inference fails in an unexpected way. The solution is to provide a type argument to the map
method.
For Example:
snapshot.data.documents.map<Widget>((document) {
return new ListTile(
title: new Text(document['name']),
subtitle: new Text("Class"),
);
}).toList()
Because of map
is followed by toList
and because there is no way to type annotate the return of a closure.
This Solution Worked for me. hope it will also work for you.
Solution 2 : Converting Map
to Widget
Here I am solving this error by converting Map
to Widget
.
children: snapshot.map<Widget>((data) =>
_buildListItem(context, data)).toList(),
Solution 3 : Cast dynamic List to List With specific Type
We can Cast dynamic List to List With specific Type:
List<'YourModel'>.from(_list.where((i) => i.flag == true));
So Hope This Above 3 Solution Will Work For You Too. So it’s all About type ‘List’ is not a subtype of type ‘List’ in Flutter error. Hope this tutorial helped you a lot. Comment below Your thoughts and your queries. And Also Comment on your suggestion here.
Also Read
- How to hide letter counter from the bottom of TextField in Flutter
- Converting TimeStamp in Flutter, Flutter Date time to Timestamp
- Flutter Pub: Expected a key while parsing a block mapping. path:
- Flutter Firestore causing D8: Cannot fit requested classes in a single dex file (# methods: 71610 > 65536) in Android Studio
- Flutter Error: MediaQuery.of() called with a context that does not contain a MediaQuery