close

[Solved] type List is not a subtype of type List

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?

  1. 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 to Widget will also solve your error.

  2. 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 to Widget 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

Leave a Comment

%d bloggers like this: