简体   繁体   中英

How to get value from enum in flutter

I get a response from API and i deffine a enum like this

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:

  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?

If you will try StatusEnum.PURPLE.toString() , you will get a string 'StatusEnum.PURPLE'.
For this purpose, of getting only value, I prefer and recommend using 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 .

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:

enum Status {
  failed,
  pending,
}

You can use name property on the enum.

String status = Day.failed.name; // 'failed'

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.

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