Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert enum to List<String>

I have enum class and I want to show the enum data to ListView. Can anyone tell how to do that?

enum TableType { circle, square, rectangle }
like image 768
kartheeki j Avatar asked May 16 '26 07:05

kartheeki j


2 Answers

Possible duplicate of this

You can do

List<String> values = TableType.values.map((e) => e.name).toList();

More about name extension

like image 159
Yeasin Sheikh Avatar answered May 17 '26 21:05

Yeasin Sheikh


You could use something like this to be able to use enum values inside your UI:

enum TableType { circle, square, rectangle }

class ExmaplePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView(
      children: TableType.values
          .map(
            (tt) => _Table(tt),
          )
          .toList(
            growable: false,
          ),
    );
  }
}

class _Table extends StatelessWidget {
  final TableType type;
  _Table(this.type);

  @override
  Widget build(BuildContext context) {
    return Text(
      type.name,
    );
  }
}

But it would be great if you could provide more information about problem you are trying to solve or even provide code example that you tried to use.

like image 21
David Sedlář Avatar answered May 17 '26 20:05

David Sedlář



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!