简体   繁体   English

flutter 中的高级开关盒

[英]Advanced switch case in flutter

I can write switch statement in iOS/Swift like this,我可以像这样在iOS/Swift中编写 switch 语句,

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?但是如何在 flutter 中编写高级 switch 语句?

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.我认为 Dart 2.17 添加了增强的枚举,所以这是 dart 中最接近你 r 在这里问的东西。 You can find more details at this thread How do I add Methods or Values to Enums in Dart?您可以在此线程中找到更多详细信息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 freezed package 是最好的解决方案

https://pub.dev/packages/freezed#map https://pub.dev/packages/freezed#map

Consider this class:考虑这个 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那么您可以使用map而不是switch来实现相同的目标

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它是swift中强大enum的最佳选择

As @Bashar noticed in his answer, Dart 2.17 added enhanced enums, which is the closest thing in Dart to what you have asked.正如@Bashar 在他的回答中注意到的那样,Dart 2.17 添加了增强的枚举,这是 Dart 中最接近您所要求的内容。

There is a small article about enhanced enums in Dart 2.17.Dart 2.17 中有一篇关于增强枚举的小文章。

In Dart, enum values are compile-time constants, so you can not instantiate them in runtime with a non-constant value.在 Dart 中,枚举值是编译时常量,因此您不能在运行时使用非常量值实例化它们。

The closest thing you can write with standard enums in Dart is:您可以在 Dart 中使用标准枚举编写的最接近的内容是:

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: output 将是:

None
null
1 case with int - 12
12
2 case with String - String
String
3 case with bool - true
true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM