Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. type ‘List<dynamic>’ is not a subtype of type ‘List<Widget>’ in flutter. So today Here I come with all possible solutions for this error.
We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.
How type ‘List<dynamic>’ is not a subtype of type ‘List<Widget>’ Error Occurs ?
When I run my code I got following error in my stacktrack.
type 'List<dynamic>' is not a subtype of type 'List<Widget>'
Here Is my code. Where I got This Error.
return ListView(
children: snapshot.data.finaldata.map((document) {
return ListTile(
title: Text(finaldata['number']),
);
}).toList(),
);
How to Solve type ‘List<dynamic>’ is not a subtype of type ‘List<Widget>’ Error?
- How to Solve type 'List
' is not a subtype of type 'List ' Error? 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
toWidget
will also solve your error. - type 'List' is not a subtype of type 'List'
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
toWidget
will also solve your error.
Solution 1
The problem is maps require type inference and you are not passing any type that’s why inference fails in an unexpected way. The solution is to assign a type to the map method. Here I am giving type to map<Widget>.
return ListView(
children: snapshot.data.finaldata.<strong>map<Widget></strong>((document) {
return ListTile(
title: Text(finaldata['number']),
);
}).toList(),
);
Solution 2
I have solve my problem by converting Map
to Widget
children: snapshot.map<Widget>((data) =>
_buildListItem(context, data)).toList(),
Solution 3
Also You can Cast dynamic List to List With specific Type:
List<'YourModel'>.from(_list.where((i) => i.flag == true));
Solution 4
You can also use ListView.builder()
constructor to convert each item into a widget.
ListView.builder(
itemCount: finalData.length,
itemBuilder: (context, index) {
final apiData = finalData[index];
return ListTile(
title: apiData.buildTitle(context),
subtitle: apiData.buildSubtitle(context),
);
},
);
Summery
So, It’s All About this error. I hope this tutorial helps you to solve your error. Please Comment Below if You stucks anywhere with my code.
Also Check Out Below Tutorials
- Flutter (Dart): Exceptions caused by rendering / A RenderFlex overflowed
- Playstore error: App Bundle contains native code, and you’ve not uploaded debug symbols
- Flutter: Could not find the built application bundle at build/ios/iphonesimulator/Runner.app
- Plugin project :firebase_core_web not found. Please update settings.gradle.
- How to fix HttpException: Connection closed before full header was received
Leave a Reply