在提供者类中使用设置器(setters)和获取器(getters)的用途是什么?

huangapple go评论62阅读模式
英文:

what is the use of using setters and getters in provider Class?

问题

以下是要翻译的内容:

It is a pretty basic question. I started off using the provider class without any getters and setters, but recently I see a lot of examples of this class using getters and setters. I remember using them in Java or something and have completely removed them as I didn't find any need. However, can someone explain if it is actually necessary? I have no code to post as it's a pretty generic question.

For the sake of an example: a typical provider class, do I need setters and getters for customMarkers?


List<Marker> customMarkers = [];

  void removeCustomMarker(String name) {
    customMarkers.removeWhere(
        (element) => element.key.toString().substring(3, 7) == name);
    notifyListeners();
  }
英文:

It is a pretty basic questions, i started off using the provider class without any getters and setters, but recently i see a lot of examples of this class using getters and setters, i remember using them in java or sth and have completely removed them as i didn't find any need?
How ever can some one explain if it is actually necessary? I have no code to post as it pretty generic question.

for the sake of an example: a typical provider class, do i need to setters and getters fro customMarkers ?


List&lt;Marker&gt; customMarker = [];

  void removeCustomMarker(String name) {
    customMarker.removeWhere(
        (element) =&gt; element.key.toString().substring(3, 7) == name);
    notifyListeners();
  }

答案1

得分: 2

你想要使用getter和setter,因为它允许您分离每个实例所需的内容,例如,如果您只获取值,就不应通知监听器。

class AppProvider {
  
  List<Marker> _customMarkers = [];

  get customMarkers {
    return _customMarkers;
  }
  set customMarkers(String name) {
    _customMarkers.removeWhere((element) => element.key.toString().substring(3, 7) == name);
    notifyListeners();
  }

}

class ExampleWidget extends StatelessWidget {

  appProvider = new AppProvider();

  // widget将由监听它的提供者包含
  @override
  Widget build(BuildContext context) {

    return Column(
      children: [
        Text(
          appProvider.customMarkers;
        ),
        TextButton(
          onTap: () => {
            appProvider.customMarkers = 'new Value';
          },
        )
      ],
    );
  }
}
英文:

You want to use getter/setters because it lets you split what is needed for each instance, for example you shouldn't notify listeners if you are only getting the value.

class AppProvider {

    List&lt;Marker&gt; _customMarkers = [];

    get customMarkers {
        return _customMarkers;
    }
    set customMarkers(String name) {
        _customMarker.removeWhere((element) =&gt; element.key.toString().substring(3, 7) == name);
        notifyListeners();
        return customMarkers;
    }

}


class ExampleWidget extends StatelessWidget {

    appProvider = new AppProvider();

    // widget would be contained by your provider that would be listening
    @override
    Widget build(BuildContext context) {

        return Column(
            children: [
                Text(
                    appProvider.customMarkers;
                ),
                TextButton(
                    onTap: () =&gt; {
                        appProvider.customMarkers = &#39;new Value&#39;;
                    },
                )
            ],
        );
    }
}

huangapple
  • 本文由 发表于 2023年6月5日 22:48:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定