Flutter 省道扩展-方法';xxx和x27;isn';t为类型';xxx和x27;
我在一个具有静态函数Flutter 省道扩展-方法';xxx和x27;isn';t为类型';xxx和x27;,flutter,dart,Flutter,Dart,我在一个具有静态函数toSelectList()的enumm上进行了此扩展,但是当我尝试使用此扩展时,无法识别toSelectList()。我以前在VSCode中遇到过这个问题,所以我知道IDE有时会显示警告,但是警告应该消失,而这次不是。唯一有效的修复方法是使用扩展,而不是使用要扩展的原始枚举。我的代码: 枚举和扩展名(杂货店\商品\类别\枚举.dart): import'包:vepo/src/presentation/widgets/form_widgets/select/common/ca
toSelectList()
的enumm上进行了此扩展,但是当我尝试使用此扩展时,无法识别toSelectList()
。我以前在VSCode中遇到过这个问题,所以我知道IDE有时会显示警告,但是警告应该消失,而这次不是。唯一有效的修复方法是使用扩展,而不是使用要扩展的原始枚举。我的代码:
枚举和扩展名(杂货店\商品\类别\枚举.dart
):
import'包:vepo/src/presentation/widgets/form_widgets/select/common/card_select_list_item/card_select_list_item.dart';
枚举杂货店项目类别{
肉
乳品的
烘烤,
调味品,
烹饪,
糖果店,
甜点
健康食品,
}
扩展GroceryItemCategoryExtension在GroceryItemCategoryEnum上的扩展{
静态常量项={
GroceryItemCategoryEnum.meat:
CardSelectListItem(标签:“肉类”,id:1,iconCodePoint:0xf814),
GroceryItemCategoryEnum.dairy:
CardSelectListItem(标签:“Dairy”,id:2,iconCodePoint:0xf7f0),
GroceryItemCategoryEnum.烘焙:
CardSelectListItem(标签:“烘焙”,id:3,iconCodePoint:0xf563),
食品类调味品:
CardSelectListItem(标签:“调味品”,id:4,iconCodePoint:0xf72f),
食品杂货分类烹饪:
CardSelectListItem(标签:“烹饪”,id:5,iconCodePoint:0xe01d),
食品杂货项目分类:糖果:卡片选择列表项目(
标签:“糖果店”,id:6,iconCodePoint:0xf819),
GroceryItemCategoryEnum.甜点:
CardSelectListItem(标签:“甜点”,id:7,iconCodePoint:0xf810),
GroceryItemCategoryEnum.healthFood:
CardSelectListItem(标签:“健康食品”,id:8,iconCodePoint:0xf787),
};
CardSelectListItem获取项目=>items[此];
静态列表toSelectList(){
返回常数[
CardSelectListItem(标签:“肉类”,id:1,iconCodePoint:0xf814),
CardSelectListItem(标签:“Dairy”,id:2,iconCodePoint:0xf7f0),
CardSelectListItem(标签:“烘焙”,id:3,iconCodePoint:0xf563),
CardSelectListItem(标签:“调味品”,id:4,iconCodePoint:0xf72f),
CardSelectListItem(标签:“烹饪”,id:5,iconCodePoint:0xe01d),
CardSelectListItem(标签:“糖果”,id:6,iconCodePoint:0xf819),
CardSelectListItem(标签:“甜点”,id:7,iconCodePoint:0xf810),
CardSelectListItem(标签:“健康食品”,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())
];
}
import'程序包:vepo/src/domain/constants/screery_item_categories_enum.dart';
@凌驾
列出createItemCategoriesFormWidgets({BuildContext context}){
返回[
VpSelectMultipleCards(列表项: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)
无法将参数类型“dynamic”分配给参数类型“List”。dart(参数类型不可分配)
没有为类型“GroceryItemCategoryEnum”定义方法“toSelectList”。
尝试将名称更正为现有方法的名称,或定义名为“toSelectList.dart”的方法(未定义的\u方法)
如何使用此扩展方法
toSelectList()
,而IDE不会显示错误。您可以在扩展中声明静态方法,但它们在扩展本身上是静态的
因此,您应该在GroceryItemCategoryExtension上调用toSelectList()
,而不是在GroceryItemCategory上调用
字符串
的基本示例:
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";
}
您可以在扩展中声明静态方法,但它们在扩展本身上是静态的
因此,您应该在GroceryItemCategoryExtension上调用toSelectList()
,而不是在GroceryItemCategory上调用
字符串
的基本示例:
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";
}
澄清一下:扩展向要扩展的类型的实例添加语法糖,因此GroceryItemCategoryEnum.meat.toSelectList()
应该可以工作。如果要使用类型调用它,则可以直接调用扩展(GroceryItemCategoryExtension.toSelectList()
)。当然,如果您这样做,那么将其作为一个扩展而不是一个独立函数就没有多大意义了。澄清一下:扩展为要扩展的类型的实例添加了语法糖,因此GroceryItemCategoryEnum.meat.toSelectList()
应该可以工作。如果要使用类型调用它,则可以直接调用扩展(GroceryItemCategoryExtension.toSelectList()
)。当然,如果您这样做,那么将它作为一个扩展而不是一个独立函数就没有多大意义了。