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 ...

Get Dart 1 for Everyone now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.