Dart 使用 setter 进行从概念上更改属性的操作

Dol*_*rma 5 analytics dart flutter

在下面的代码中,当我尝试设置值时,ValueNotifier我得到了分析信息,

info: Use a setter for operations that conceptually change a property. (use_setters_to_change_properties at 
[XXXXX] lib\service\providers\value_notifier_scroll_to_selected_page_position.dart:6) 
Run Code Online (Sandbox Code Playgroud)

我的代码:

class ScrollToSelectedPagePosition {
  final ValueNotifier<int> position = ValueNotifier<int>(0);

  void scrollToPosition(int selectedPagePosition){
    position.value = selectedPagePosition; // <--- Getting info here
  }
}
Run Code Online (Sandbox Code Playgroud)

我如何为此定义设置器?

jam*_*lin 3

我想它希望你这样做:

set scrollToPosition(int selectedPagePosition) {
  position.value = selectedPagePosition;
}
Run Code Online (Sandbox Code Playgroud)

然后调用者会使用scrollToPosition = position而不是scrollToPosition(position).

假设scrollToPosition()执行一些滚动操作,我个人更喜欢它看起来像函数调用,并且我会使用以下命令抑制 lint

// ignore: use_setters_to_change_properties
Run Code Online (Sandbox Code Playgroud)

(但是,如果将其命名为position,则 setter 可能更合适。)