0

With abstract classes I can require that a class that implements my abstract class has some method. I can type <T extends MyAbstractClass> and I can call every Method on objects of type T that I have specified in MyAbstractClass. But how can I make sure that some amount of classes have the factory constructor fromJson()?

EinEsellesEniE
  • 129
  • 1
  • 10
  • You cannot specify that kind of requirement in Dart so the only way to do it is to take the type `dynamic` and try call `fromJson()` on this input. This is actually what Dart does internally for the `toJson()` method which can be called when converting a object to JSON. Yes, it will fail if the method does not exist so you need to catch the exception and handle it if that is important. – julemand101 May 14 '21 at 13:30
  • 3
    Does this answer your question? [Dart - Abstract constructors](https://stackoverflow.com/questions/58223010/dart-abstract-constructors) – Nisanth Reddy May 14 '21 at 13:31

0 Answers0