简体   繁体   English

Dart:当尝试更改此列表的一个元素时,作为列表的类中的属性正在调用其 getter

[英]Dart: A property in a class which is a list is calling its getter when trying to change one element of this list

I have an abstract class with public properties settingsDefaultSchedule , settingsScheduleInAdvance , settingsPostoneDuration , planData , homePageNumOfHours , savedTasks defined using get and set keywords.我有一个抽象类,其中包含使用getset关键字定义的公共属性settingsDefaultSchedulesettingsScheduleInAdvancesettingsPostoneDurationplanDatahomePageNumOfHourssavedTasks When changing one of those properties _toUpdate variable is set to true and corresponding private property with an underscore is updated.当更改其中一个属性时, _toUpdate变量设置为true并更新带有下划线的相应私有属性。

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 .其中一个公共属性 ( savedTasks ) 是一个列表,我试图更改其中一个元素,但是当我这样做时, _toUpdate变量仍然是false I tried to debug this code and I noticed that Dart is calling getter savedTasks instead of the setter.我试图调试这段代码,我注意到 Dart 正在调用 getter savedTasks而不是 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?明显的解决方法是只更改整个列表,但我很好奇为什么这不起作用,为什么 Dart 在更改列表属性的一个元素时调用 getter?

I'm sorry if I've missed something obvious, I'm just a beginner, and also forgive me for improper terminology.如果我错过了一些明显的东西,我很抱歉,我只是一个初学者,也请原谅我使用不当的术语。

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.设置List的一个元素与设置整个List不同 - List对象保持其身份,同时更新存储在其中的数据 - 因此,不调用 setter。

One possible solution is to introduce a new static method, updateTaskDataAtIndex(int index, TaskData task) , something like this:一种可能的解决方案是引入一种新的静态方法updateTaskDataAtIndex(int index, TaskData task) ,如下所示:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM