O'Reilly logo

Dart 1 for Everyone by Chris Strom

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Methods

Methods are simply named functions inside a class. The following is a method that renders the current view by assigning the el instance variable’s innerHTML to the result of a template.

classes/methods.dart
 
class​ ComicsView {
 
// ...
 
render() {
 
el.innerHtml = template(collection);
 
}
 
}

Inside the class, methods may be invoked by calling the method directly—prepending the method with this. is not required like it is in JavaScript (though it would still work). In the previous example, template is an instance method that is invoked with the view’s collection property.

In Dart, it is generally considered good practice to prepend the return type of a method or prepend void if the method does not return anything.

classes/method_with_type.dart ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required