[英]How to get value from enum in flutter
I get a response from API and i deffine a enum like this我收到了 API 的回复,我定义了一个这样的枚举
enum StatusEnum { EMPTY, STATUS, PURPLE }
final statusEnumValues = EnumValues({
"کنسل شده": StatusEnum.EMPTY,
"اتمام": StatusEnum.PURPLE,
"در انتظار پرداخت": StatusEnum.STATUS
});
but when I use this in my code I get the error "The argument type "StatusEnum" can not be assigned to the parameter type 'String'. this my code that i use:但是当我在我的代码中使用它时,我收到错误“无法将参数类型“StatusEnum”分配给参数类型'String'。这是我使用的代码:
Consumer<GetOrders>(
builder: (context, data, child) {
return ListView.builder(
physics: NeverScrollableScrollPhysics(),
shrinkWrap: true,
itemCount: getOrders.allData.length,
itemBuilder: (context, index) {
try {
return Column(
crossAxisAlignment:
CrossAxisAlignment.end,
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.end,
children: [
Text(
data.allData[index].statuse,
style: TextStyle(
fontFamily:
"Vazir",fontWeight: FontWeight.bold),
),
],
),
],
);
} on Exception catch (_) {
print('never reached');
return null;
}
});
},
)
can anyone help mo to solve this problem?任何人都可以帮助 mo 解决这个问题吗?
If you will try StatusEnum.PURPLE.toString()
, you will get a string 'StatusEnum.PURPLE'.如果您尝试
StatusEnum.PURPLE.toString()
,您将得到一个字符串“StatusEnum.PURPLE”。
For this purpose, of getting only value, I prefer and recommend using enum_to_string package.为此,为了获得唯一的价值,我更喜欢并推荐使用enum_to_string package。 It allows to get enum value as a String, without your enum name, ie
YourEnum.value
-> EnumToString.toString(YourEnum.value)
=> value
.它允许将枚举值作为字符串获取,而无需您的枚举名称,即
YourEnum.value
-> EnumToString.toString(YourEnum.value)
=> value
。
The below code will describe the implementation of the enum:下面的代码将描述枚举的实现:
enum Day {
monday, tuesday, wednesday, thursday, friday, saturday, sunday
}
void validateDescribeEnum() {
assert(Day.monday.toString() == 'Day.monday');
assert(describeEnum(Day.monday) == 'monday');
}
Update Dart 2.15:更新 Dart 2.15:
enum Status {
failed,
pending,
}
You can use name
property on the enum.您可以在枚举上使用
name
属性。
String status = Day.failed.name; // 'failed'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.