How can I inherit static methods in dart/flutter?

817

No you cannot do that. This excerpt is from the official Dart language specification:

enter image description here

Share:
817
Paul
Author by

Paul

Updated on January 04, 2023

Comments

  • Paul
    Paul over 1 year

    Is it possible in Dart/Flutter to inherit static methods or factories? Or do I need to workaround this by creating an instance to access that static method?

    My case is that I want to serialize an object but need to access a general parse function for them.

    abstract class Foo {
    
      static Foo parse(); //Error, must have a body
    
      Foo parse();//No error but need to call Foo().parse(); by creating an instance.
    }
    

    I want to create by using json so is bad practice and against performance to create a new instance to return another one?

    class InheritedFoo {
    
      final String string;
    
      InheritedFoo(this.string);
    
      @override
      Foo parse() {
        return InheritedFoo("some string");
      }
    }
    

    Is it maybe possible to use a singleton to save performance (call InheritedFoo.inst.parse() )?

    • jamesdlin
      jamesdlin about 2 years
      It is not possible. static members are not part of the class interface. static methods are just a scope-restricted form of global functions. Your workaround Foo().parse() would not be much better since it would not be taking advantage of polymorphism anyway (except in the case that Foo() is a factory constructor that could return different derived instances at runtime).
    • Paul
      Paul about 2 years
      Ok, understandable, but can I maybe create an instance of Foo by itself and use the inherited to access that instance and call parse? factory constructor is not possible either to inherit. Or is there maybe a way to use reflections like java?
    • Luis Utrera
      Luis Utrera about 2 years
      I think this is not possible with dart, unfortunately
    • jamesdlin
      jamesdlin about 2 years
      @Paul I don't understand what you mean. It might help if you describe what problem you're actually trying to solve. Inheritance is usually used for polymorphism, and polymorphism does not make sense without an object instance. You might be able to accomplish what you want by having the caller supply a callback that constructs the desired object. However, if you're trying to invent your own JSON parser, you really should just use package:built_value or package:json_serializable or something similar.
    • Paul
      Paul about 2 years
      I want to call a function on an class without creating an instance. But every member of that superclass must override this function. (This function should return a new instance of it by json given) But currently I found no way to do this. (e.g. make abstract static Foo parse() and override that in Inherited Foo to be able to call InheritedFoo.parse()
    • jamesdlin
      jamesdlin about 2 years
      You can't override functions without having instances. Let's say you had multiple classes that derived from a single base class and both overrode a static method. How would that possibly work? How would you know which override should be called without explicitly specifying which derived class to use? And once you have to explicitly specify the derived class, it's no different than not having overrides. Maybe what you want is to have a callback that can be assigned to different things depending on the context.
    • Paul
      Paul about 2 years
      Can't you use the runtimyType to get this information (theoretically)?. If you have the type you can know which method from which class you have to call? What about the external keyword?