Home > database >  Dart: A property in a class which is a list is calling its getter when trying to change one element
Dart: A property in a class which is a list is calling its getter when trying to change one element

Time:12-22

I have an abstract class with public properties settingsDefaultSchedule, settingsScheduleInAdvance, settingsPostoneDuration, planData, homePageNumOfHours, savedTasks defined using get and set keywords. When changing one of those properties _toUpdate variable is set to true and corresponding private property with an underscore is updated.

abstract class GetData{

  // The data
  static List<double>? _settingsDefaultSchedule;
  static int? _settingsScheduleInAdvance;
  static int? _settingsPostoneDuration;
  static PlanData? _planData;
  static int? _homePageNumOfHours;
  static List<TaskData>? _savedTasks;

  // A variable that controls the updating of the data
  static bool _toUpdate = false;



  // Getter for the instance variables
  static List<double> get settingsDefaultSchedule => _settingsDefaultSchedule!;
  static int get settingsScheduleInAdvance => _settingsScheduleInAdvance!;
  static int get settingsPostoneDuration => _settingsPostoneDuration!;
  static PlanData get planData => _planData!;
  static int get homePageNumOfHours => _homePageNumOfHours!;
  static List<TaskData> get savedTasks => _savedTasks!;



  // Setters for the instance variables
  static set settingsDefaultSchedule(List<double> value) {
    if (_settingsDefaultSchedule == null || value != _settingsDefaultSchedule){
      _toUpdate = true;
      _settingsDefaultSchedule = value;
    } 
  }
  static set settingsScheduleInAdvance(int value) {
    if (_settingsScheduleInAdvance == null || value != _settingsScheduleInAdvance){
      _toUpdate = true;
      _settingsScheduleInAdvance = value;
    }
  }
  static set settingsPostoneDuration(int value) {
    if (_settingsPostoneDuration == null || value != _settingsPostoneDuration){
      _toUpdate = true;
      _settingsPostoneDuration = value;
    }
  }
  static set planData(PlanData value) {
    if (_planData == null || value != _planData){
      _toUpdate = true;
      _planData = value;
    }
  }
  static set homePageNumOfHours(int value) {
    if (_homePageNumOfHours == null || value != _homePageNumOfHours){
      _toUpdate = true;
      _homePageNumOfHours = value;
    }
  }
  static set savedTasks(List<TaskData> value) {
    if (_savedTasks == null || value != _savedTasks){
      _toUpdate = true;
      _savedTasks = value;
    }
  }

  // ...
}

One of those public properties (savedTasks) is a list, and I'm trying to change one of its elements, but when I do that the _toUpdate variable is still false. I tried to debug this code and I noticed that Dart is calling getter savedTasks instead of the setter.

GetData.savedTasks[index] = TaskData(
  name: GetData.savedTasks[index].name, 
  duration: GetData.savedTasks[index].duration, 
  importance: newListIndex, 
  key: GetData.savedTasks[index].key,
  everydayTask: GetData.savedTasks[index].everydayTask,
  everydayTaskTime: GetData.savedTasks[index].everydayTaskTime,
  oneTimeTask: GetData.savedTasks[index].oneTimeTask,
);

The obvious fix is to just change the whole list, but I'm curious why is this not working, why is Dart calling getter when changing one of the elements of a list property?

I'm sorry if I've missed something obvious, I'm just a beginner, and also forgive me for improper terminology.

CodePudding user response:

Setting an element of a List is not the same as setting the whole List - The List object keeps its identity, while the data stored within is updated - for that reason, the setter is not called.

One possible solution is to introduce a new static method, updateTaskDataAtIndex(int index, TaskData task), something like this:

static void updateTaskDataAtIndex(int index, TaskData taskData) {
  if (_savedTasks == null || index < 0 || index >= _savedTasks.length) {
    return;
  }

  _savedTasks[index] = taskData;
  _toUpdate = true;
}
  • Related