Title: type 'List<dynamic>' is not a subtype of type 'List<Todo>'
Tags: flutter;dart;dio;flutter-getx
Question: I use dio with getx to get list of todo data but I see this message
type 'List<dynamic&gt;' is not a subtype of type 'List<Todo&gt;'
when i go reach this line
List responseBode =  rsp.data['data'];
what is happen in this line and how to fix it , thanks
service class updated with converting to the list but still the same issue
1- the first class for controller
2- the second for the service class
class TodoListController extends GetxController {
//var lstTask = List<dynamic&gt;.empty(growable: true).obs;
var items = List<Todo&gt;.empty(growable: true).obs;
var page = 1.obs;
var totalRows = 10.obs;
var isDataProcessing = false.obs;
var isMoreDataAvailable = true.obs;
var isSearching = false.obs;
var isLoading = false.obs;
var isCompleted = true.obs;
var errorMsg = ''.obs ;
var http = new HttpUtils();
// For Pagination
ScrollController scrollController = ScrollController();
@override
onInit() {
super.onInit();
// Fetch Data
getTodo(page);
// Fetch Data
getTodo(var page) {
try {
isMoreDataAvailable(false);
isDataProcessing(true);
AppServices().getTodo(page).then((resp) {
isDataProcessing(false);
resp.forEach(( dynamic json) {
items.add(Todo.fromJson(json));
});
print(resp);
//items.addAll(resp);
}, onError: (err) {
isDataProcessing(false);
AppServices().showSnackBar(&quot;Error&quot;, err.toString(), Colors.red);
});
} catch (exception) {
isDataProcessing(false);
AppServices().showSnackBar(&quot;Exception&quot;, exception.toString(), Colors.red);
// Get More data
getMoreTodo(var page) {
try {
AppServices().getTodo(page).then((resp) {
if (resp.length &gt; 0) {
isMoreDataAvailable(true);
} else {
isMoreDataAvailable(false);
AppServices().showSnackBar(&quot;Message&quot;, &quot;No more items&quot;, Colors.lightBlueAccent);
}
items.addAll(resp);
}, onError: (err) {
isMoreDataAvailable(false);
AppServices().showSnackBar(&quot;Error&quot;, err.toString(), Colors.red);
});
} catch (exception) {
isMoreDataAvailable(false);
AppServices().showSnackBar(&quot;Exception&quot;, exception.toString(), Colors.red);
class AppServices extends GetConnect {
var http = new HttpUtils();
// Fetch Data
Future<List<Todo&gt;&gt; getTodo(var id) async {
try {
Response rsp =  await http.get(AppUrl.get_todo, {'current_page': id});
print(rsp.data['data']);
if (rsp.data[&quot;response_status&quot;] != '400') {
List<Todo&gt; responseBode = rsp.data['data'].map ((todo) =&gt; Todo.fromJson(todo)).toList();
return responseBode;
} else {
return rsp.data[&quot;response_message&quot;] ;
}
}catch(exception)
return Future.error(exception.toString());

Here is the accepted answer: The problem is that the list of dynamic can't be automatically converted to a list of Todo's.
You can convert the list like this:
List<Todo&gt; responseBode = rsp.data['data'].map ((todo) =&gt; Todo.fromJson(todo)).toList();
Then you can implement the Todo named contructor like this:
class Todo {
//Your todo class
String name;
Todo.fromJson (data) {
// conversion here
name = data[&quot;name&quot;] ?? null;
Comment for this answer: still the same , still see the same exception
Comment for this answer: also i have a todo model
Comment for this answer: can you post the updated version of the code?

Here is another answer: This might be fixable with a simple type cast. I noticed you're using Dio with GetConnect. Nothing wrong with that per se but I can't imagine why you'd need both.
Since you're already in the GetX world here's how it would look just using GetConnect. This was how I could test because I'm not sure where HttpUtils comes from.
class AppServices extends GetConnect {
// var http = new HttpUtils();
// Fetch Data
Future<List<Todo&gt;&gt; getTodo(var id) async {
try {
Response rsp = await httpClient.get(AppUrl.get_todo, {'current_page': id});
print(rsp.body['data']);
if (rsp.statusCode != 400) {
List<Todo&gt; responseBode = rsp.body['data'] as List<Todo&gt;; // casting type here
return responseBode;
} else {
return rsp.body[&quot;response_message&quot;];
}
} catch (exception) {
return Future.error(exception.toString());
If this doesn't work
List<Todo&gt; responseBode = rsp.body['data'] as List<Todo&gt;; // casting type here
Then you could also probably use return Future<List&gt; instead.
Then that would turn the previous line into this
List responseBode = rsp.body['data'];
GetConnect handles json decoding for you so when using that, you don't need to manually do any more json work.
Edit: To answer your question in the comments about adding an auth to header in GetConnect:
You can do something like this.
final url = 'http://...'
void _setUrl() {
httpClient.baseUrl = url;
httpClient.addRequestModifier((request) {
request.headers['apikey'] = yourApiKey;
return request;
});
After that you can just call
final response = await httpClient.get(url);
And your key will be in the url.
Then it returns a regular map and you're good to go.
Comment for this answer: thank you but this not working if remove this line because it is a dio wraper , var http = new HttpUtils(); i use dio because i have two questions about getconnect but no one example on the internet although i google it , 1- how to add basic auth to header , 2-how to pass many params to the same request
Comment for this answer: as you see here i have the ability to pass this
get(AppUrl.get_todo, {'current_page': id})
and it will converted to http://example.com/todo_list?current_page=1
Comment for this answer: the json library returns a map which can’t be automatically casted to a class, you need a named constructor for that. At least as far as i know.
Comment for this answer: @Ale sorry I should clarified that json comment was specific to `GetConnect` which decodes the json itself and returns a regular map. I'll update the answer just for clarity. @user1080247 looks like you have a solution but see updated answer about adding auth in headers with `GetConnect`. I agree there is very little info out there about it.

Here is another answer: You can just use GetConnect without Dio.
Your AppServices would look Like:
Future<List<Todo&gt;&gt; getTodo(var id) async {
final response =  await http.get(AppUrl.get_todo, query:{'current_page': id}, decoder: Todo.listFromJson);
if(response.hasError){
return Future.error(response.statusText);
return response.body;
Your model will look like:
class Todo {
String name;
Todo.fromJson (data) {
name = data[&quot;name&quot;] ?? null;
static List<Todo&gt; listFromJson(dynamic str) =&gt; List<Todo&gt;.from((str as List<dynamic&gt;).map((x) =&gt; Todo.fromJson(x)));
Now it will directly return the List of Todo objects.
Usage might be something like this (in the controller):
final todoList = <Todo&gt;[].obs;
onInit()async{
await getTodos();
Future<void&gt; getTodos()async{
try{
final todos=await appServices.getTodo(1);
todoList.assignAll(todos);
} catch (e){
// handle errors here. eg. show snackbar

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:03/27/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<flutter><dart><dio><flutter-getx>', 'Title': "type 'List<dynamic>' is not a subtype of type 'List<Todo>'", 'ViewCount': 577, 'id': 66836460, 'user_AboutMe': '<p>php mysql developer</p>\n\n<p><a href="http://webeasystep.com/blog/view_article/dynamic_bootstrap_star_rating_with_codeigniter" rel="nofollow">codeigniter tutorial</a></p>\n\n<p><a href="http://webeasystep.com/blog/view_article/Codeigniter_Clean_URL" rel="nofollow">codeigniter clean url</a></p>\n\n<p><a href="http://webeasystep.com/tag/tag_page/codeigniter-tutorials" rel="nofollow">codeigniter-tutorials</a></p>\n', 'user_AccountId': 1084093.0, 'user_DisplayName': 'user1080247', 'user_Reputation': 1036.0, 'user_Views': 410.0, 'user_id': 1080247.0}}