Home > OS >  Unable to update data. type '_InternalLinkedHashMap<String, Object>' is not a subtyp
Unable to update data. type '_InternalLinkedHashMap<String, Object>' is not a subtyp

Time:08-21

I have one main class Data and the other class Tasks. I get the data from API by

 final response = await http.get(Uri.parse("some url"));
    final r = jsonDecode(response.body);
List<Data> list = [];
    for (Map i in r) {
           list.add(Data.fromJson(i));
         } 
List<Data> dataValue = [];
dataValue  = list!.cast<Data>();
List ListdataValue  =[];
ListdataValue =(dataValue?.tasks != null)?(dataValue?.tasks):[];

Now I can read the data title into the tasks by

Text(Listtask[some iterable index].title)

Error statment: I need to update the list I fetch from API. but when I add some data it shows an error. what I am missing here?

var taskData={
               "title": "Title of the task",
     };

Listtask.add(taskData);

Error in console:

type '_InternalLinkedHashMap<String, Object>' is not a subtype of type 'Tasks' of 'value'

Data class:

 class Data {
      int? id;
      String? title;
      List<Tasks>? tasks;
      Data(
          {this.id, this.title, this.tasks,});
    
      Data.fromJson(Map<dynamic, dynamic> json) {
        id = json['id'];
        title = json['title'];
        if (json['tasks'] != null) {
          tasks = <Tasks>[];
          json['tasks'].forEach((v) {
            tasks!.add( Tasks.fromJson(v));
          });
        }
        
      }
      Map<String, dynamic> toJson() {
        final Map<String, dynamic> data = new Map<String, dynamic>();
        data['id'] = id;
        data['title'] =title;
        if (this.tasks != null) {
          data['tasks'] = tasks!.map((v) => v.toJson()).toList();
        }
        return data;
      }
    }

Tasks class:

class Tasks {
  int? id;
  String? title;
  Tasks(
      {this.id,
        this.title,
       });
  Tasks.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    title = json['title'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['title'] = this.title;
    return data;
  }
}

Json:

{
   "id":01,
   "title":"string",
   "tasks":[
      {
         "title":"Some title value"
      },
      {
         "title":"Some title value for 2"
      }
   ]
}

CodePudding user response:

maybe you need to apply Tasks.fromJson():

var taskData = {
  "title": "Title of the task",
};

Listtask.add(Tasks.fromJson(taskData));

and, if necessary, apply as Map<String, dynamic>

  • Related