簡體   English   中英

Dart 擴展 - 沒有為類型“xxx”定義方法“xxx”

[英]Dart extension - The method 'xxx' isn't defined for the type 'xxx'

我已經在具有 static function toSelectList toSelectList()的枚舉上進行了此擴展,但是當我嘗試使用擴展時,無法識別toSelectList() 我以前在 VSCode 中遇到過這個問題,所以我知道 IDE 有時可能會顯示警告,但警告應該 go 消失,而這次不是。 唯一有效的解決方法是使用擴展而不是使用被擴展的原始枚舉。 我的代碼:

枚舉和擴展名( 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),
    ];
  }
}

嘗試使用它:

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

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

編譯時錯誤:

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)

如何在 IDE 顯示錯誤的情況下使用此擴展方法 toSelectList toSelectList()

您可以在擴展中聲明 static 方法,但它們在擴展本身上是 static。

因此,您應該在GroceryItemCategoryExtension而不是GroceryItemCategory上調用 toSelectList toSelectList()

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"; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM