I can write switch statement in iOS/Swift
like this,
enum Test {
case none
case first(Int)
case second(String)
case third(Bool)
var value:String {
switch self {
case .none:
return "None"
case .first(let int):
return "First case with Integer value - \(int)"
case .second(let string):
return "Second case with String value - \(string)"
case .third(let bool):
return "Third case with Boolean value - \(bool)"
}
}
}
class Testing {
func execute() {
print(Test.none.value)
print(Test.first(10).value)
print(Test.second("Vatsal").value)
print(Test.third(true).value)
}
}
But how to write an advanced switch statement in flutter?
Kindly, share your knowledge.
I think Dart 2.17 Added the enhanced enums, So thats the closest thing in dart to what u r asking here. You can find more details at this thread How do I add Methods or Values to Enums in Dart?
To get the same behaviour I would implement it like this:
void main() {
print(Test.none().value);
print(Test.first(10).value);
print(Test.second("Vatsal").value);
print(Test.third(true).value);
}
class Test {
dynamic _value;
Test._([this._value]);
factory Test.none() => Test._();
factory Test.first(int i) => Test._(i);
factory Test.second(String s) => Test._(s);
factory Test.third(bool b) => Test._(b);
String get value {
switch (_value.runtimeType) {
case Null: return "None";
case int: return "First case with Integer value - $_value";
case String: return "Second case with String value - $_value";
case bool: return "Third case with Boolean value - $_value";
default: throw Exception();
}
}
}
freezed
package is the best solution
https://pub.dev/packages/freezed#map
Consider this class:
@freezed
class Model with _$Model {
factory Model.first(String a) = First;
factory Model.second(int b, bool c) = Second;
}
then you could use map
instead of switch
to achieve the same goal
var model = Model.first('42');
print(
model.map(
first: (First value) => 'first ${value.a}',
second: (Second value) => 'second ${value.b} ${value.c}'
),
); // first 42
it's the best alternative for the powerful enum
in swift
As @Bashar noticed in his answer, Dart 2.17 added enhanced enums, which is the closest thing in Dart to what you have asked.
There is a small article about enhanced enums in Dart 2.17.
In Dart, enum values are compile-time constants, so you can not instantiate them in runtime with a non-constant value.
The closest thing you can write with standard enums in Dart is:
enum EnhancedEnum {
none(),
first(12),
second('String'),
third(true);
const EnhancedEnum([this.value = null]);
final dynamic value;
@override
String toString() {
switch (this) {
case EnhancedEnum.none:
return 'None';
default:
return '$index case with ${this.value.runtimeType} - $value';
}
}
}
void main() {
print(EnhancedEnum.none);
print(EnhancedEnum.none.value);
print(EnhancedEnum.first);
print(EnhancedEnum.first.value);
print(EnhancedEnum.second);
print(EnhancedEnum.second.value);
print(EnhancedEnum.third);
print(EnhancedEnum.third.value);
}
The output will be:
None
null
1 case with int - 12
12
2 case with String - String
String
3 case with bool - true
true
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.