skip to Main Content

I have recently migrated from Dart 2 to Dart 3 and have noticed that Class Modifiers have been added, including interface class. They work perfectly fine when setting properties, but when it comes to methods, it throws an error stating that I need to convert the class to abstract. Based on logic, I understand that interfaces should not have method bodies. Here’s an example:

Works fine:

interface class Foo {
  late String bar;
}

Throws dartconcrete_class_with_abstract_member:

interface class Foo {
  late String bar;
  
  // 'getFormattedBar' must have a method body because 'Foo' isn't abstract.
  // Try making 'Foo' abstract, or adding a body to 'getFormattedBar'.
  // (dartconcrete_class_with_abstract_member)
  String getFormattedBar(); 
}

How I can declare interface methods in Dart 3?

I was hoping to define methods in the interface (as per the definition of an interface, of course) and then later establish their concrete implementations with their respective bodies in the implementing classes. Instead, I’m being forced to place an empty body in the interface, which doesn’t make any sense. It would be ideal to have something like:

interface class Foo {
  late String bar;
  String getFormattedBar(); 
}

3

Answers


  1. Chosen as BEST ANSWER

    To include methods in your interface, you must use abstract interface class instead of just interface class. This will allow you to include methods without a body in an interface (which is slightly redundant).

    Example:

    abstract interface class Foo {
      late String bar;
      
      String getFormattedBar();   // Works fine :)
    }
    
    

  2. In Dart Dart 2, you declare your interface like this;

    abstract class Foo {
      late String bar;
    
      String getFormattedBar(); 
    }
    

    Then implement like this;

    class IFoo implements Foo {
      //implement the methods
    }
    

    In Dart 3:

    abstract interface class Foo {
      late String bar;
    
      String getFormattedBar(); 
    }
    
    Login or Signup to reply.
  3. The interface keyword makes sure that you can only implement the class, and not mixin nor extend the class. Or in other words, it makes sure that you cannot inherit from the class.

    You can still construct the class if the interface class modifier is the only class modifier you use. This means that the methods need to have a concrete implementation.

    // Can be constructed and implemented, but not extended nor mixed in.
    interface class Foo {
    
      // Implementation needed
      String bar() {
        return 'baz';
      }
    }
    

    If you also add the abstract class modifier then you can not construct the class and the methods does not have to have a concrete implementation.

    // Can not be constructed.
    // Can be implemented, but not extended nor mixed in.
    abstract interface class Foo {
    
      // Implementation should be left out
      String bar();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search