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

Hello Guys, how are you all? Hope you all are fine. You might face type ‘List<dynamic>’ is not a subtype of type ‘List<int>’ Flutter as this error in a flutter. 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<int>’ Flutter Error Occurs ?

I am trying to run my code but o found this error in my stacktrack

‘List<dynamic>’ is not a subtype of type ‘List<int>’

At this below line. Here Is I Am Facing Above Error.

        generateID = jsonMap["json_ids"];

Solutions

Question : How to Solve type ‘List<dynamic>’ is not a subtyoe of type ‘List<int>’ ?
Answer : You need to bring the value to its required type before you can assign it. So, all you need to do is Just cast your variable to Integer.

Solution 1

types in JSON maps or lists don’t have concrete generic types. generateID requires a List<int> not a List (or List<dynamic>), therefore you need to bring the value to its required type before you can assign it. So That just follow below step.

Change

generateID = jsonMap["json_ids"];

to

generateID = jsonMap["json_ids"].cast<int>();

This Solution Worked for me.

Solution 2

A shorten way to handle is

generateID = (jsonMap["json_ids"] as List)?.map((e) => e as int)?.toList();

Solution 3

We could also initializing new List instead of casting. same as below.

var generateIdsFromJson = jsonMap['json_ids'];
List<int> generateIdList = new List<int>.from(generateIdsFromJson);

// then you can use gendreIdsList to the mapping function

generateID = generateIdList

Summery

So it’s all About All possible solutions. Hope this above all solution helped you a lot. Comment below Your thoughts and your queries. Comment Below on your suggestion.

Check Out Below Article

Milan Dhameliya is an Information Technology Engineer. By profession, he is a Full Stack developer with knowledge of multiple back-end platforms (e.g., PHP, Node.js, Python) and frontend Flutter frameworks.

Leave a Comment