concept getter in category dart

appears as: getters, getter, A getter, getter
Dart in Action

This is an excerpt from Manning's book Dart in Action.

The getter and setter syntax is also useful because you can use the fields of a class the same way you use getters and setters. Thus a class designer can expose the property (such as greeting, in listing 1.2) and later change it to use a getter and setter (such as in name in the example) without needing to change the calling code.

Fields, getters, and setters in Dart can be used interchangeably with no change to the calling code. This means the line

itemContainer.children.add(packItem.uiElement);

has no knowledge as to whether it’s reading a value from a getter or from a property. A getter (or setter) is a method that’s prefixed with the get (or set) keyword. A getter must take no parameters, and a setter must take a single parameter, as in the following code:

Once again, you’ll use a setter and a getter to represent the isPacked property. There’s a requirement to perform an action (updating the UI) when you set the isPacked value, which you can do by adding the additional CSS class .packed to the uiElement property. The .packed CSS class specifies text-decoration: line-through in the CSS styles that you added to your HTML file earlier.

First, add the private _isPacked property and the setter and getter, as shown next.

Listing 3.7. Adding the isPacked property, getter, and setter
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage