I'm trying to add task to list folderTask. The list looks like this:
List<FolderTask> folderTask = [];
List<Task> task = [];
folderTask = [FolderTask(name: 'Personal', task: task)];
And it return RangeError. The method that I use to add the item inside list Task is this:
void setTask(int index, newTask) {
var specificIndex = folderTask
.indexWhere((element) => element.name == folderTask[index].name);
if (folderTask.isEmpty) {
createInitialData();
} else if (specificIndex != -1) {
folderTask[index] =
FolderTask(name: folderTask[index].name, task: newTask);
// folderTask[index] = newTask;
}
updateDatabase();
notifyListeners();
}
What I'm trying to achieve is on my first page, user be able to add new folder and inside that folder, user be able to add new task. But it return the rangeeror. Is it because of this:
int? index = db.folderTask.indexOf(widget.folderTask);
or is it because of I use Provider with ChangeNotifier and Consumer makes the rangeerror?
class Task {
String? name;
String? note;
String? reminderDate;
String? repeat;
String dueDate;
bool? isChecked = false;
String? reminderTime;
List path = [];
Task(
{required this.name,
required this.note,
required this.dueDate,
required this.reminderDate,
required this.repeat,
required this.path,
required this.reminderTime,
this.isChecked});
factory Task.fromJson(json) {
return Task(
name: json['name'],
note: json['note'],
dueDate: json['dueDate'],
reminderDate: json['reminderDate'],
reminderTime: json['reminderTime'],
repeat: json['repeat'],
path: json['path'],
isChecked: json['isChecked']);
}
Map<String, dynamic> toJson() => {
"name": name,
"note": note,
"dueDate": dueDate,
"reminderDate": reminderDate,
"reminderTime": reminderTime,
"repeat": repeat,
"path": path,
'isChecked': isChecked
};
}
CodePudding user response:
try to add task like this
List<FolderTask> folderTask = [];
List<Task> task = [];
folderTask.add[task] // this line
CodePudding user response:
folderTask[index] = FolderTask(name: folderTask[index].name, task: newTask);
It's seems like you trying to add newTask in your list, try doing it with folderTask.add(newTask)
.
Couse you havent index-th element in your list and you actualy want to change it. Here is reason.
void main() {
List<int> test = [];
test[1] = 1;
}
try this in DartPad and you will see your mistake. And be carefull with your List types.