Let’s see how to create a list of objects that response from the UI. First we will create a model and we will call it task_model.dart
class Task {
int? id;
String? title;
String? note;
int? isCompleted;
String? date;
String? startTime;
String? endTime;
int? color;
int? remind;
String? repeat;
Task({
this.id,
this.title,
this.note,
this.isCompleted,
this.date,
this.startTime,
this.endTime,
this.color,
this.remind,
this.repeat,
});
Task.fromJson(Map<String, dynamic> json) {
id = json['id'];
title = json['title'];
note = json['note'];
isCompleted = json['isCompleted'];
date = json['date'];
startTime = json['startTime'];
endTime = json['endTime'];
color = json['color'];
remind = json['remind'];
repeat = json['repeat'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['title'] = this.title;
data['date'] = this.date;
data['note'] = this.note;
data['isCompleted'] = this.isCompleted;
data['startTime'] = this.startTime;
data['endTime'] = this.endTime;
data['color'] = this.color;
data['remind'] = this.remind;
data['repeat'] = this.repeat;
return data;
}
}
In the above model, you can change your field anyway, you want. But you should know that, that’s how you create a basic model class.
Since we have the model, now we can go ahead create list of objects using it. We want to create responsive or reactive list.
So we must use RxList and Obs with the list. Let’s create the list
final RxList<Task> taskList = List<Task>.empty().obs;
Now your taskList is responsive or reactive. With this you can add objects(task type) to and remove it.
If you want to add objects to taskList
taskList.assignAll(tasks.map((data) => new Task.fromJson(data)).toList());
Here data is type below. So data could be list of Maps
List<Map<String, dynamic>>