You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何实现枚举值映射为类属性值,并通过字符串触发枚举输出对应值

Solution to Map String to Enum and Class Property in Dart

Let's break down how to meet all your requirements step by step. We'll enhance the enum to handle string-to-enum conversion, add a direct mapping to the MaterialWidgets class properties, and ensure you get the expected output.

Step 1: Enhance the Enum with Parsing and Mapping

First, we'll update MaterialWidgetsEnum to include a method that converts a string to the corresponding enum value, plus a getter that returns the matching static property from MaterialWidgets:

enum MaterialWidgetsEnum {
  radio,
  button,
  navigator;

  // Convert a string to the matching enum value (case-insensitive)
  static MaterialWidgetsEnum? fromString(String value) {
    try {
      return values.firstWhere(
        (enumVal) => enumVal.name.toLowerCase() == value.toLowerCase(),
      );
    } catch (_) {
      return null; // Return null if no match, or throw an error if you prefer strict validation
    }
  }

  // Map the enum value to the corresponding static property in MaterialWidgets
  String get widgetValue {
    switch (this) {
      case MaterialWidgetsEnum.radio:
        return MaterialWidgets.radio;
      case MaterialWidgetsEnum.button:
        return MaterialWidgets.button;
      case MaterialWidgetsEnum.navigator:
        return MaterialWidgets.navigator;
    }
  }
}

class MaterialWidgets {
  static String radio = "radio";
  static String button = "button";
  static String navigator = "navigator";
}

Step 2: Implement the Logic in Main

Now, in your main function, we'll use the string variable x to get the enum value, then retrieve both the enum's name (for your print requirement) and the corresponding MaterialWidgets property:

void main() {
  // Define your string variable x as required
  String x = "radio";

  // Get the enum value that matches x
  MaterialWidgetsEnum? selectedWidget = MaterialWidgetsEnum.fromString(x);

  if (selectedWidget != null) {
    // Print the enum's name (outputs "radio" as expected)
    print(selectedWidget.name);

    // Get and print the corresponding MaterialWidgets property value
    print(selectedWidget.widgetValue); // Also outputs "radio"
  } else {
    print("No matching widget found for '$x'");
  }
}

Key Explanations:

  • String to Enum Conversion: The fromString method searches through the enum values to find a match for your string x. It’s case-insensitive, so it works even if x is "Radio" or "RADIO".
  • Enum to Class Property Mapping: The widgetValue getter uses a switch case to return the exact static string from MaterialWidgets that corresponds to the enum value.
  • Expected Output: When you run print(selectedWidget.name), it outputs "radio" because that’s the identifier of the enum member. If you want to directly print the MaterialWidgets property value, use print(selectedWidget.widgetValue).

Alternative: Directly Get Class Property from String

If you don’t need the enum and just want to fetch the MaterialWidgets property from x, you can use a helper function:

String? getMaterialWidgetValue(String key) {
  switch (key.toLowerCase()) {
    case "radio":
      return MaterialWidgets.radio;
    case "button":
      return MaterialWidgets.button;
    case "navigator":
      return MaterialWidgets.navigator;
    default:
      return null;
  }
}

// Usage in main:
print(getMaterialWidgetValue(x)); // Outputs "radio"

But since your requirements explicitly mention using the enum, the first approach is the best fit.

内容的提问来源于stack exchange,提问作者Mariam Albarghouti

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 12:42:48