Home > Software engineering >  Convert enum to List<String>
Convert enum to List<String>

Time:07-11

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 }

CodePudding user response:

Possible duplicate of this

You can do

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

More about name extension

CodePudding user response:

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.

  • Related