Search code examples
flutterdartdioflutter-getx

type 'List<dynamic>' is not a subtype of type 'List<Todo>'


I use dio with getx to get list of todo data but I see this message

type 'List<dynamic>' is not a subtype of type 'List<Todo>'

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>.empty(growable: true).obs;
  var items = List<Todo>.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("Error", err.toString(), Colors.red);
      });
    } catch (exception) {
      isDataProcessing(false);
      AppServices().showSnackBar("Exception", exception.toString(), Colors.red);
    }
  }

  // Get More data
   getMoreTodo(var page) {
    try {
        AppServices().getTodo(page).then((resp) {
        if (resp.length > 0) {
          isMoreDataAvailable(true);
        } else {
          isMoreDataAvailable(false);
          AppServices().showSnackBar("Message", "No more items", Colors.lightBlueAccent);
        }
        items.addAll(resp);
      }, onError: (err) {
        isMoreDataAvailable(false);
        AppServices().showSnackBar("Error", err.toString(), Colors.red);
      });
    } catch (exception) {
      isMoreDataAvailable(false);
      AppServices().showSnackBar("Exception", exception.toString(), Colors.red);
    }
  }
}

   

class AppServices extends GetConnect { var http = new HttpUtils();

          // Fetch Data
  Future<List<Todo>> getTodo(var id) async {
    try {
      Response rsp =  await http.get(AppUrl.get_todo, {'current_page': id});
      print(rsp.data['data']);
      if (rsp.data["response_status"] != '400') {
        List<Todo> responseBode = rsp.data['data'].map ((todo) => Todo.fromJson(todo)).toList();
        return responseBode;
      } else {
        return rsp.data["response_message"] ;
      }
    }catch(exception)
    {
      return Future.error(exception.toString());
    }
  }
}

Solution

  • 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> responseBode = rsp.data['data'].map ((todo) => 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["name"] ?? null;
        }
    }