如何让 Dart 全局检测不同文件的枚举扩展名?

发布于 2025-01-11 17:41:13 字数 3353 浏览 1 评论 0原文

我想为从放大模型生成器生成的枚举创建扩展,因此我在不同的文件中创建了扩展(因为枚举是自动生成的),但是当我尝试使用它时,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

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();
    }
  }
}

错误 扩展无法识别

注意: 宠物模型使用 PetsType 枚举作为属性类型。

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.

Auto Generated Enum 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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

§普罗旺斯的薰衣草 2025-01-18 17:41:13

扩展方法是语法糖。它们不是类(或者在本例中为enum)接口的一部分,并且无法自动检测和使用。 (Dart 编译器如何知道在哪里查找可用扩展?)

您可以做的是:

  1. 将自动生成的 enum 定义移动到私有 .dart 文件(< a href="https://dart.dev/tools/pub/package-layout#implementation-files" rel="nofollow noreferrer">约定是将私有实现文件放在 src/ 中子目录)。
  2. 将您的扩展放入一个公共文件中,该文件导入是该私有文件,导出枚举
  3. 期望消费者导入公共文件。

这应该(主要)确保枚举的使用者也同时使用扩展。 (这不能保证;病态的消费者如果明确选择的话可以直接导入私有文件,并且他们也可以在导入<时显式隐藏扩展名然而,与故意表现得病态的人作斗争无论如何都是浪费时间。)

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. (How would the Dart compiler even know where to look to detect available extensions?)

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).
  2. Put your extension in a public file that imports that private file and exports the enum.
  3. Expect that consumers import the public file.

That should (mostly) ensure that consumers of the enum also consume the extension at the same time. (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 importing the public file. However, fighting people intentionally trying to be pathological is a waste of time anyway.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文