Dart Flutter联合字符串类型 [英] Dart Flutter Union String types

查看:176
本文介绍了Dart Flutter联合字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在打字稿中,如果我要确保变量是"this"或"that",我会这样做:

In typescript if I want to make sure a variable is 'this' or 'that' I would do:

type ThisThat = 'this'|'that'

然后我可以在其他界面中使用这种类型:

I could then use this type in another interface like :

interface B{
   text: ThisThat;
}

我如何使用类在飞镖/颤振中执行相同的操作?还是有其他方法可以做到?我查了一下dart枚举,看来在dart中使用String枚举并不是很简单.

How would I do this same thing in dart/flutter using classes? Or is there a different way to do it? I looked up dart enums and it seems that using String enums in dart isn't exactly straight-forward.

推荐答案

Dart尚无此功能.已经对此进行了一些讨论,但我不记得是否仍在考虑该功能或该功能何时发布.

Dart does not have such a feature yet. There has been some discussions about it but I can't remember if the feature are still considered or when it comes.

因此,到目前为止,我们需要执行语言提供的功能并制定自己的解决方案".根据我的经验,人们最想要的实际上是枚举,因为字符串值本身在映射后并不重要.

So for now we need to do what the language provides and make our own "solution". In my experience, what people most want is actually enums since the string value itself are not important after it is mapped.

枚举也是最接近联合字符串类型的,因为我们有有限数量的允许的ing,编译器可以验证它们在例如切换语句.

Enums are also what comes closest to union string types since we have a finite amount of allowed stings which the compiler can verify we takes care of in e.g. switch-statements.

因此,如果我们想花一点点时间,可以做这样的事情:

So we can do something like this if we want to make it a little fancy:

enum ThisThat { THIS, THAT }

ThisThat parseThisThat(String value) => ThisThat.values.firstWhere(
    (element) => element.toString() == 'ThisThat.${value.toUpperCase()}');

extension ThisThatValue on ThisThat {
  String get stringValue => toString().split('.')[1].toLowerCase();
}

void main() {
  ThisThat tt = parseThisThat('this');
  print(tt.stringValue); // this

  tt = parseThisThat('sheep'); // Unhandled exception: Bad state: No element
}

就个人而言,如果允许值的数量受到限制,我会适当地使解析更加明确,这也很容易引发您自己的异常类型:

Personally, I would properly make it more explicit the parsing if the amount of allowed values are limited which also makes it easy to throw you own type of exception:

ThisThat parseThisThat(String value) {
  if (value == 'this') {
    return ThisThat.THIS;
  } else if (value == 'that') {
    return ThisThat.THAT;
  } else {
    throw Exception('$value is not a valid string value for ThisThat');
  }
}

或使用开关:

ThisThat parseThisThat(String value) {
  switch (value) {
    case 'this':
      return ThisThat.THIS;
    case 'that':
      return ThisThat.THAT;
    default:
      throw Exception('$value is not a valid string value for ThisThat');
  }
}

扩展名的目的是使我们可以再次获取字符串值.在Dart中,枚举值上的 toString()例如返回 ThisThat.THIS .在大多数情况下,这很好,但是如果您想将其转换回去,您可以添加扩展方法,在这种情况下,该方法会在枚举值上提供 stringValue 属性.

The purpose of the extension is so we can get the string value again. In Dart, the toString() on a enum value will e.g. return ThisThat.THIS. This is properly fine in most cases but if you want to convert it back you can add the extension method which in this case gives a stringValue property on the enum value.

(还应注意,如果允许的字符串不是 this ,则可能会更容易,因为您不能拥有名称为 this 的枚举值,因为 this 是关键字,但是允许 THIS ,所以这就是我的枚举为大写的原因.:)

(Should also note that it could be easier if the allowed string was not this since you cannot have a enum value with the name this since this is a keyword. But THIS is allowed so that is why my enum was upper cased. :)

这篇关于Dart Flutter联合字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆