搜索插件、代码仓与 APP 分类

插件详情v2.2.1

enum_to_string

更好的枚举到字符串的转换。Dart 在调用 enum.toString 时使用令人困扰的 EnumName.ValueName 语法,此包解决了这个问题。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
17.7万
喜欢数
331
Pub 分数
150

安装插件

flutter pub add enum_to_string

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个 Dart 包,通过消除冗长的 EnumName.ValueName 语法,简化了枚举到字符串的转换。它提供了一种简洁直观的方法来获取枚举值的字符串表示形式,提高了可读性并减少了样板代码。非常适合经常处理枚举且希望获得更简单 toString() 替代方案的开发者。该包支持所有主要平台,并可无缝集成到 Flutter 项目中。

适合场景

  • 在 UI 中将枚举值转换为可读字符串
  • 简化枚举数据的序列化
  • 改进日志中枚举状态的代码可读性
  • 在 API 请求或配置中使用枚举字符串
  • 避免重复的 EnumName.ValueName 引用

主要特点

  • 干净的枚举到字符串转换
  • 消除冗长的枚举语法
  • 支持所有 Flutter 平台
  • 最小化设置和集成
  • 提高代码可维护性

适合对象

  • 使用枚举的 Flutter 开发人员
  • 需要枚举字符串序列化的项目
  • 注重代码清晰性和可读性的团队
  • 频繁使用枚举的应用程序
  • 寻求减少样板代码的开发者

使用建议

  • 需要手动注册枚举
  • 可能不支持所有高级枚举模式
  • 仅限基本字符串表示需求
  • 不适合复杂的枚举转换
  • 依赖于正确的枚举实现

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

enum_to_string

CI

Better conversion of ENUMs to string - It also can handle converting back again!

What it does

It takes an enum such as:

enum TestEnum { testValue1 }

and converts it to

testValue1

Also handles camel case

Input enum TestEnum { testValue1 } Output Test value 1

You can also capitalize all words using the capitalizeWords flag: Input enum TestEnum { testValue1 } Output with capitalizeWords: true: Test Value 1

Usage

import 'package:enum_to_string/enum_to_string.dart';

enum TestEnum { testValue1, testValue2 };

convert(){
    String result = EnumToString.convertToString(TestEnum.testValue1);
    //result = 'testValue1'

    String result = EnumToString.convertToString(TestEnum.testValue1, camelCase: true);
    //result = 'Test value 1'

    String result = EnumToString.convertToString(TestEnum.testValue1, camelCase: true, capitalizeWords: true);
    //result = 'Test Value 1'

    EnumToString.fromString(TestEnum.values, "testValue1"); //Enum
    // TestEnum.testValue1

    EnumToString.fromString(TestEnum.values, "Test Value 1", camelCase: true);
    // TestEnum.testValue1

    List<String> result = EnumToString.toList(TestEnum.values);
    //result = ['testValue1','testValue2'],

    List<String> result = EnumToString.toList(TestEnum.values, camelCase: true);
    //result = ['Test value 1','Test value 2'],
    
    List result = EnumToString.fromList(TestEnum.values, ["ValueOne", "Value2"]); //List<Enum>
    //result = [TestEnum.valueOne, TestEnum.value2];
}

Contributing

Any pull requests / extensions welcome, this was just an annoying thing I needed to fix a couple of times so viola! a package was born.

The project uses GitHub Actions for CI (migrated from Travis CI), which automatically runs the following checks on all pull requests:

  • Dart formatting
  • Static analysis
  • Tests

You can run all checks locally before submitting a PR using the provided script:

./tool/check.sh

Or run them individually:

# Get dependencies
dart pub get

# Format code
dart format --output=none --set-exit-if-changed .

# Run static analysis
dart analyze

# Run tests
dart test

The CI pipeline requires all tests to pass. Please ensure your changes include appropriate test coverage.

Enhanced Enum Support (Dart 2.17+)

Starting from version 2.2.0, this package supports Dart's enhanced enums with custom string mappings. This is particularly useful when you need enum string representations that can't be valid Dart identifiers.

enum Rating {
  G,
  PG13('PG-13'),  // Custom mapping with hyphen
  R,
  unrated;        // Falls back to normal EnumToString behavior

  final String? value;
  const Rating([this.value]);
}

void main() {
  // Custom mapped values use their explicit mapping
  print(EnumToString.convertToString(Rating.PG13));  // Prints: PG-13
  
  // Unmapped values use normal EnumToString behavior
  print(EnumToString.convertToString(Rating.unrated));  // Prints: unrated
  
  // All other EnumToString features work as expected
  print(EnumToString.fromString(Rating.values, 'PG-13') == Rating.PG13);  // true
  print(EnumToString.toList(Rating.values));  // ['G', 'PG-13', 'R', 'unrated']
}

This feature is particularly useful when:
- You need special characters in your enum strings (like hyphens or spaces)
- Your enum strings need to match an external API or format exactly
- You want some enum values to have custom string representations while others use the default behavior

所属分类

源码仓库

使用此插件的项目