2

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 }
Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
kartheeki j
  • 2,206
  • 5
  • 27
  • 51
  • 2
    Does this answer your question? [Dart How to get the name of an enum as a String](https://stackoverflow.com/questions/29567236/dart-how-to-get-the-name-of-an-enum-as-a-string) – nvoigt Jul 11 '22 at 08:18

2 Answers2

4

Possible duplicate of this

You can do

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

More about name extension

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
2

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.

David Sedlář
  • 706
  • 5
  • 15