Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Enhanced Enums cause Flutter app to hang when used in widget

I am trying to upgrade my code to use the new enhanced enum feature of dart 2.17 and flutter 3.0.5.

Here is my enum:

enum Permission {
  first(1, "first"),
  second(2, "second");

  const Permission(this.id, this.name);

  final int id;
  final String name;
}

Usage, called in build() of a stateful widget:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

String name = Permission.first.name;

When I run my program everything compiles and I get no error messages, but the program hangs on a white screen and constantly reloads, never making it to the home screen. If I comment out the line where I access the name of the permission, everything loads and runs properly. Not sure why accessing the enum property causes the program to break. Any reason this is happening?

>Solution :

Don’t use name as a filed on enum. It is already having a .name extension on enum.

enum Permission {
  first(1, "first"),
  second(2, "second");

  const Permission(this.id, this.value);

  final int id;
  final String value; //change it to something else
}

Now flutter clean and rebuild the app.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading