如何实现枚举值映射为类属性值,并通过字符串触发枚举输出对应值
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
fromStringmethod searches through the enum values to find a match for your stringx. It’s case-insensitive, so it works even ifxis "Radio" or "RADIO". - Enum to Class Property Mapping: The
widgetValuegetter uses a switch case to return the exact static string fromMaterialWidgetsthat 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 theMaterialWidgetsproperty value, useprint(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

