Duck-Typing in Dart with implements

Interfaces are programming language constructs that describe at a high level how classes will behave. That is, if a class implements an interface, it must define the methods and instance variables that are declared in the interface.

In Dart, everything is an interface, which is another way of saying that there are no explicit interfaces. Any class in Dart—abstract or concrete—is a potential interface. Our ComicsCollection class could implement the built-in Iterable abstract class.

classes/implements.dart
 
class​ ComicsCollection ​implements​ Iterable {
 
void​ forEach(fn) {
 
models.forEach(fn);
 
}
 
 
int ​get​ length => models.length;
 
// ...
 
}

What this tells other Dart classes is that there is ...

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.