22

I was wondering which is the best way to extend the CustomEvent class, a class which has only one factory constructor. I tried doing the following and ran into an issue with the super constructor :

class MyExtendedEvent extends CustomEvent {
  int count;

  factory MyExtendedEvent(num count) {
    return new MyExtendedEvent._internal(1);
  }

  MyExtendedEvent._internal(num count) {
    this.count = count;
  }
}

but I can't get it working. I always run into :

unresolved implicit call to super constructor 'CustomEvent()'

If i try chaning the internal constructor to :

MyExtendedEvent._internal(num count) : super('MyCustomEvent') {
  this.count = count;
}

I end up with :

'resolved implicit call to super constructor 'CustomEvent()''.

I'm not sure what I'm doing wrong - but I guess the problem is that the CustomEvent has only one constructor which is a factory constructor (as doc says - http://api.dartlang.org/docs/releases/latest/dart_html/CustomEvent.html)

What is the best way to extend a CustomEvent, or any class of this form?

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
markovuksanovic
  • 15,676
  • 13
  • 46
  • 57
  • It seems like it isn't possible at all to extend CustomEvent. I tried to outsmart it, and ended up with a runtime error instead: Internal error: '[filename]': Error: line 3 pos 7: class 'MyEvent' is trying to extend a native fields class, but library '[same filename]' has no native resolvers". I don't know if one can work around this or not, sorry. – MarioP Sep 02 '13 at 08:21

4 Answers4

23

You can't directly extend a class with a factory constructor. However you can implement the class and use delegation to simulate the extension.

For instance :

class MyExtendedEvent implements CustomEvent {
  int count;
  final CustomEvent _delegate;

  MyExtendedEvent(this.count, String type) :
    _delegate = new CustomEvent(type);

  noSuchMethod(Invocation invocation) =>
      reflect(_delegate).delegate(invocation);
}

NB : I used reflection here to simplify the code snippet. A better implementation (in regard of performances) would have been to define all methods like method1 => _delegate.method1()

Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
5

Unfortunately, you can't extend a class if it only has factory constructors, you can only implement it. That won't work well with CustomEvent though since it's a DOM type, which is also why it only has factory constructors: the browser has to produce these instances, the Dart object is just a wrapper. If you try to implement CustomElement and fire one, you'll probably get an error.

Justin Fagnani
  • 10,483
  • 2
  • 27
  • 37
3

You can now use the extension feature of Dart (from Dart 2.7) to get a similar behaviour if you just want to add some methods to the existing class.

extension MyExtensionEvent on CustomEvent {
  void increaseCount() => count++;
}

and then when you want to use your newly created extension you just import it and then call an instance of the original class but with one of your newly created methods:

import event_extension.dart

final event = CustomEvent(1);
event.increaseCount();
spydon
  • 9,372
  • 6
  • 33
  • 63
0

If the class has only factory constructors(named or unnamed) cannot be used for extending. (But you can use this class to implement subclasses)

So if you want to extend a class by a superclass, The superclass must be needed to have at least one generative constructor.

dilshan
  • 2,045
  • 20
  • 18