Flutter中如何编写高级switch语句?
在Flutter(Dart)中实现类似Swift关联值枚举的Switch逻辑
Dart原生枚举不支持携带关联值,要实现你Swift代码里的功能,需要用**密封类(Sealed Class)**来模拟,结合Dart 3.0+的模式匹配,可以实现几乎一致的switch体验。下面提供两种实现方式:
一、手动实现(无第三方依赖)
通过定义抽象基类+子类的方式,模拟Swift的关联值枚举:
1. 定义密封类结构
// 抽象基类,标记为密封类(Dart 3.0+支持) sealed class Test { String get value; } // 对应Swift的.none case class TestNone implements Test { @override String get value => "None"; } // 对应Swift的.first(Int) case,携带Int类型关联值 class TestFirst implements Test { final int intValue; TestFirst(this.intValue); @override String get value => "First case with Integer value - $intValue"; } // 对应Swift的.second(String) case,携带String类型关联值 class TestSecond implements Test { final String stringValue; TestSecond(this.stringValue); @override String get value => "Second case with String value - $stringValue"; } // 对应Swift的.third(Bool) case,携带Bool类型关联值 class TestThird implements Test { final bool boolValue; TestThird(this.boolValue); @override String get value => "Third case with Boolean value - $boolValue"; }
2. 使用示例
void execute() { print(TestNone().value); print(TestFirst(10).value); print(TestSecond("Vatsal").value); print(TestThird(true).value); } // 或者用switch模式匹配(更贴近Swift写法) String getTestValue(Test test) { switch (test) { case TestNone(): return "None"; case TestFirst(intValue): // 直接解构关联值 return "First case with Integer value - $intValue"; case TestSecond(stringValue): return "Second case with String value - $stringValue"; case TestThird(boolValue): return "Third case with Boolean value - $boolValue"; } }
二、使用Freezed包简化实现
如果觉得手动写子类繁琐,可以用freezed包自动生成密封类代码,语法更接近Swift的枚举写法:
1. 添加依赖
在pubspec.yaml中添加:
dependencies: freezed_annotation: ^2.4.1 dev_dependencies: build_runner: ^2.4.8 freezed: ^2.4.7
2. 定义Freezed类
import 'package:freezed_annotation/freezed_annotation.dart'; part 'test.freezed.dart'; @freezed class Test with _$Test { // 定义各个case,对应Swift的关联值枚举 const factory Test.none() = _None; const factory Test.first(int intValue) = _First; const factory Test.second(String stringValue) = _Second; const factory Test.third(bool boolValue) = _Third; // 对应Swift中的value计算属性 String get value { return when( none: () => "None", first: (intValue) => "First case with Integer value - $intValue", second: (stringValue) => "Second case with String value - $stringValue", third: (boolValue) => "Third case with Boolean value - $boolValue", ); } }
3. 生成代码
在终端运行命令生成相关代码:
flutter pub run build_runner build
4. 使用示例
void execute() { print(const Test.none().value); print(const Test.first(10).value); print(const Test.second("Vatsal").value); print(const Test.third(true).value); } // 同样支持switch模式匹配 String getTestValue(Test test) { switch (test) { case _None(): return "None"; case _First(intValue): return "First case with Integer value - $intValue"; case _Second(stringValue): return "Second case with String value - $stringValue"; case _Third(boolValue): return "Third case with Boolean value - $boolValue"; } }
关键说明
- Dart 3.0+的模式匹配让switch可以直接解构密封类的关联值,语法和Swift的switch高度一致;
- 密封类保证了switch可以覆盖所有可能的case,不会出现遗漏;
- Freezed包大幅简化了密封类的编写,自动生成equals、hashCode、toString等方法,减少样板代码。
内容的提问来源于stack exchange,提问作者Vatsal Shukla
相关产品推荐
相关产品推荐

