I've made this extension on an enumm which has a static function toSelectList(), but when I try to use the extension, the toSelectList() is not recognised. I have had this issue before in VSCode, so I understand the IDE may sometimes show a warning, but the warning should go away and this time it isn't. The only fix that has worked is to use the extension and not use the original enum being extended. My code:

The enum and extension (grocery_item_categories_enum.dart):

import 'package:vepo/src/presentation/widgets/form_widgets/select/common/card_select_list_item/card_select_list_item.dart';

enum GroceryItemCategoryEnum {
  meat,
  dairy,
  baking,
  condiments,
  cooking,
  confectionary,
  dessert,
  healthFood,
}

extension GroceryItemCategoryExtension on GroceryItemCategoryEnum {
  static const items = {
    GroceryItemCategoryEnum.meat:
        CardSelectListItem(label: 'Meat', id: 1, iconCodePoint: 0xf814),
    GroceryItemCategoryEnum.dairy:
        CardSelectListItem(label: 'Dairy', id: 2, iconCodePoint: 0xf7f0),
    GroceryItemCategoryEnum.baking:
        CardSelectListItem(label: 'Baking', id: 3, iconCodePoint: 0xf563),
    GroceryItemCategoryEnum.condiments:
        CardSelectListItem(label: 'Condiments', id: 4, iconCodePoint: 0xf72f),
    GroceryItemCategoryEnum.cooking:
        CardSelectListItem(label: 'Cooking', id: 5, iconCodePoint: 0xe01d),
    GroceryItemCategoryEnum.confectionary: CardSelectListItem(
        label: 'Confectionary', id: 6, iconCodePoint: 0xf819),
    GroceryItemCategoryEnum.dessert:
        CardSelectListItem(label: 'Dessert', id: 7, iconCodePoint: 0xf810),
    GroceryItemCategoryEnum.healthFood:
        CardSelectListItem(label: 'Health Food', id: 8, iconCodePoint: 0xf787),
  };

  CardSelectListItem get item => items[this];

  static List<CardSelectListItem> toSelectList() {
    return const [
      CardSelectListItem(label: 'Meat', id: 1, iconCodePoint: 0xf814),
      CardSelectListItem(label: 'Dairy', id: 2, iconCodePoint: 0xf7f0),
      CardSelectListItem(label: 'Baking', id: 3, iconCodePoint: 0xf563),
      CardSelectListItem(label: 'Condiments', id: 4, iconCodePoint: 0xf72f),
      CardSelectListItem(label: 'Cooking', id: 5, iconCodePoint: 0xe01d),
      CardSelectListItem(label: 'Confectionary', id: 6, iconCodePoint: 0xf819),
      CardSelectListItem(label: 'Dessert', id: 7, iconCodePoint: 0xf810),
      CardSelectListItem(label: 'Health Food', id: 8, iconCodePoint: 0xf787),
    ];
  }
}

Trying to use it:

import 'package:vepo/src/domain/constants/grocery_item_categories_enum.dart';

  @override
  List<Widget> createItemCategoriesFormWidgets({BuildContext context}) {
    return [
      VpSelectMultipleCards(listItems: GroceryItemCategoryEnum.toSelectList())
    ];
  }

compile time error:

The argument type 'dynamic' can't be assigned to the parameter type 'List<CardSelectListItem>'.dart(argument_type_not_assignable)
The method 'toSelectList' isn't defined for the type 'GroceryItemCategoryEnum'.
Try correcting the name to the name of an existing method, or defining a method named 'toSelectList'.dart(undefined_method)

How do I use this extension method toSelectList() without the IDE showing an error.


Solution 1: Thierry

You can declare static methods in an extension, but they are static on the extension itself.

You should therefore call toSelectList() on GroceryItemCategoryExtension and not on GroceryItemCategory.

Basic example on String:

void main() {
  // print(String.getMyString());
  // The method 'getMyString' isn't defined for the type 'String' 
  print(StringX.getMyString());
}

extension StringX on String {
  static String getMyString() => "MyString"; 
}