简体   繁体   English

如何在 Flutter 中使用枚举作为返回值?

[英]How do I use an enum as a return value in Flutter?

I can't find an answer to this in my searches.我在搜索中找不到这个问题的答案。 How do I return an enum as the result of a function?如何返回枚举作为 function 的结果? It feels like a stupid question that I just can't figure out.这感觉像是一个愚蠢的问题,我就是想不通。

enum getCardObjectType(val object) {
    if(val is wantedObjectType)
      return CardType.Link;
  }

Use CardType as return type.使用CardType作为返回类型。

The fact that CardType is an enum doesn't change the syntax here, the type is just CardType. CardType 是一个枚举的事实并没有改变这里的语法,类型只是 CardType。

An example of an Enum is:枚举的一个例子是:

enum CardType {
    link,
    option2
}

your function should be something like this:你的 function 应该是这样的:

CardType getCardObjectType(val object) {
    if(val is wantedObjectType)
      return CardType.Link;
}

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

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