Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart: Extension method on enum doesn't work "The method isn't defined for the class"

basend this Question I created an enum in dart with an extension methode:

enum TagVisibility {
  public,
  shared,
  private,
}

extension on TagVisibility {
  String get german {
    switch(this){
      case TagVisibility.public: return "Für alle sichtbar";
      case TagVisibility.shared: return "Für alle mit demselben Tag sichtbar";
      case TagVisibility.private: return "Nur für mich sichtbar";
      default: throw Exception("enum has more cases");
    }
  }
}

But I get the error Error: The method 'german' isn't defined for the class 'TagVisibility'. when I try to call this extension method:

import 'package:prototype/models/visibility.dart';
...
 DropdownButton<TagVisibility>(
          hint: Text("Wähle die Sichtbarkeit für diesen Tag"),
          value: _visibility ?? _visibilityDefault,
          onChanged: (visibility) {
            setState(() {
              _visibility = visibility;
            });
          },
          // items: List<DropdownMenuItem<TagVisibility>>(
          items: TagVisibility.values.map((visibility) => DropdownMenuItem(
            value: visibility,
            child: Text('${visibility.german()}'), // doesn't work
            // child: Text('${visibility.toString()}'), // works, but I want the custom messages.
          ),
          ).toList(),
        ),

I have absolutly no idea what I did wrong here. Can you please explain me how I can get this working? Thx!

like image 313
DarkMath Avatar asked Nov 07 '25 18:11

DarkMath


2 Answers

I found the solution here: Import extension method from another file in Dart

There are two ways:

Solution number 1: Place the extension method in the dart file where it is used. But often it is better the place the extension method in the same file as the correspondig enumeration. So I prefer this:

Solution number 2: Give the extension a name which is different from the enum name. Code:

enum TagVisibility {
  public,
  shared,
  private,
}

extension TagGerman on TagVisibility {
  String get german {
    switch(this){
      case TagVisibility.public: return "Für alle sichtbar";
      case TagVisibility.shared: return "Für alle mit demselben Tag sichtbar";
      case TagVisibility.private: return "Nur für mich sichtbar";
      default: throw Exception("enum has more cases");
    }
  }
}
like image 178
DarkMath Avatar answered Nov 11 '25 20:11

DarkMath


You need to give the extension a name.

Dart extensions can be declared without a name, but that just means they are given a fresh private name instead, and therefore the extension is only accessible inside the library where it is declared. You basically never want an extension without a name.

If you give it a public name, then the extension can be imported into other libraries along with the enum.

like image 24
lrn Avatar answered Nov 11 '25 20:11

lrn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!