Abstract methods in Dart API reference

dart

Solution

Yes, this can be a bit confusing. While abstract classes cannot be instantiated, it is possible to make them appear to be instantiable by defining a factory constructor. This is what `Completer`, `Future` and other abstract classes do:

abstract class Completer<T> {

  factory Completer() => new _CompleterImpl<T>();

  ...
}

You can then invoke methods on the object created by the `factory` constructor. In the example above, `factory Completer()` returns a new `_CompleterImpl` object. Look at the (truncated) code of that class:

class _CompleterImpl<T> implements Completer<T> {

  final _FutureImpl<T> _futureImpl;

  _CompleterImpl() : _futureImpl = new _FutureImpl() {}

  Future<T> get future {
    return _futureImpl;
  }

  void complete(T value) {
    _futureImpl._setValue(value);
  }
  ...
}

and you see `complete()`; that is the method being invoked.

Problem

Many methods like `complete` in class `Completer` are marked "abstract", but in fact It can be directly invoked without being implemented. I'm really confused. Could anyone help me?

Original source