简体   繁体   English

如何使 Dart 在全球范围内检测来自不同文件的枚举扩展名?

[英]How to make Dart detects enum extensions from different file globally?

I wanted to create an extension for generated enum from amplify model generator, so I created the extension in different file (because the enum is autogenerated) but Dart is not detecting my extension when I try to using it.我想为从 amplify model 生成器生成的枚举创建一个扩展,所以我在不同的文件中创建了扩展(因为枚举是自动生成的)但是当我尝试使用它时 Dart 没有检测到我的扩展。

Auto Generated Enum PetsType.dart自动生成枚举 PetsType.dart

/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
*  http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

// NOTE: This file is generated and may not follow lint rules defined in your app
// Generated files can be excluded from analysis in analysis_options.yaml
// For more info, see: https://dart.dev/guides/language/analysis-options#excluding-code-from-analysis

// ignore_for_file: public_member_api_docs, file_names, unnecessary_new, prefer_if_null_operators, prefer_const_constructors, slash_for_doc_comments, annotate_overrides, non_constant_identifier_names, unnecessary_string_interpolations, prefer_adjacent_string_concatenation, unnecessary_const, dead_code

enum PetsType { DOG, CAT, BIRD, HAMSTER, FISH, OTHER }

pets_type_extension.dart pets_type_extension.dart

extension EnumPetCategoryExtension on PetsType {
  Color get color => _getColor(this);
  IconData get icon => _getIcon(this);
  String get valueString => _getUiValue(this);

  Color _getColor(PetsType category) {
    switch (category) {
      case PetsType.DOG:
        return const Color(0xffFFB56B);
      case PetsType.CAT:
        return const Color(0xffFFD977);
      case PetsType.BIRD:
        return const Color(0xff826491);
      case PetsType.HAMSTER:
        return const Color(0xffA66551);
      case PetsType.FISH:
        return const Color(0xff6DB5AD);
      case PetsType.OTHER:
        return const Color(0xffFFB56B);
    }
  }

  IconData _getIcon(PetsType category) {
    switch (category) {
      case PetsType.DOG:
        return Icons.ac_unit;
      case PetsType.CAT:
        return Icons.account_tree_outlined;
      case PetsType.BIRD:
        return Icons.airline_seat_legroom_reduced;
      case PetsType.HAMSTER:
        return Icons.bookmark_add_sharp;
      case PetsType.FISH:
        return Icons.bed;
      case PetsType.OTHER:
        return Icons.computer_sharp;
    }
  }

  String toShortString() {
    return toString().split('.').last;
  }

  String toUpperCase() {
    return toShortString().toUpperCase();
  }

  String _getUiValue(PetsType category) {
    switch (category) {
      case PetsType.DOG:
        return LocaleKeys.petTypes_dog.tr();
      case PetsType.CAT:
        return LocaleKeys.petTypes_cat.tr();
      case PetsType.BIRD:
        return LocaleKeys.petTypes_bird.tr();
      case PetsType.HAMSTER:
        return LocaleKeys.petTypes_hamster.tr();
      case PetsType.FISH:
        return LocaleKeys.petTypes_fish.tr();
      case PetsType.OTHER:
        return LocaleKeys.petTypes_others.tr();
    }
  }
}

ERROR extension unrecognized无法识别错误扩展名

Note: Pet model is using PetsType enum as a property type.注意: Pet model 使用 PetsType 枚举作为属性类型。

Extension methods are syntactic sugar.扩展方法是语法糖。 They are not part of the class (or in this case, enum ) interface and can't be automatically detected and used.它们不是 class(或在本例中为enum )接口的一部分,无法自动检测和使用。 (How would the Dart compiler even know where to look to detect available extensions?) (Dart 编译器怎么知道到哪里去检测可用的扩展?)

What you could do is:你可以做的是:

  1. Move the autogenerated enum definition to a private .dart file ( the convention is to put private implementation files in a src/ subdirectory ).将自动生成的enum定义移动到私有.dart文件( 约定是将私有实现文件放在src/子目录中)。
  2. Put your extension in a public file that import s that private file and export s the enum .将您的扩展名放在一个公共文件中,该文件import该私有文件并exportenum
  3. Expect that consumers import the public file.期望消费者import公共文件。

That should (mostly) ensure that consumers of the enum also consume the extension at the same time.这应该(主要)确保enum的消费者也同时使用扩展。 (It wouldn't be guaranteed; pathological consumers could import the private file directly if they explicitly choose to, and they also could explicitly hide the extension when import ing the public file. However, fighting people intentionally trying to be pathological is a waste of time anyway.) (不能保证;如果病态消费者明确选择,他们可以直接import私有文件,并且他们也可以在import公共文件时明确hide扩展名。但是,与故意病态的人作斗争是一种浪费反正时间。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同的 dart 文件访问变量? - How to access variables from different dart files? 如何从另一个 dart 文件访问值? - How to access values from another dart file? 如何将 If 语句添加到 main.dart 文件 - How Add a If statement to main.dart file 如何从 firebase 获取数据并将其存储到列表中以供在 dart 中进一步操作 - How to get data from the firebase and store it into a list for futher manipulation in dart 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 如何使用 flutter 和 dart 从 Cloud Firestore 检索特定数据? - how to retrieve specific data from Cloud Firestore using flutter and dart? 如何获取从 Dart Flutter Firebase 检索到的数据的值? - How to get value of data retrieved from Dart Flutter Firebase? 如何使用 dart 编程语言从 firebase 集合中返回文档引用? - How to return document references from firebase collection using dart programing language? AWS Glue:如何确保胶水爬虫始终从 S3 获取最新文件 - AWS Glue : How to make sure glue crawler always picks up the latest file from S3 如何将 map 从 InstanceClass 枚举转换为实际的 AWS 实例类型 - How to map from InstanceClass enum to actual AWS instance types
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM