Luke Pighetti
Luke Pighetti

Reputation: 4841

How to check if value is enum?

I would like to check if a value in dart is an Enum, and if so, serialize it automatically.

My sticking point is how to test if a value is an enum.

This is how I am testing for other types

if (T == int)
  val = prefs.getInt(this._key);
else if (T == double)
  val = prefs.getDouble(this._key);
else if (T == bool)
  val = prefs.getBool(this._key);
else if (T == String)
  val = prefs.getString(this._key);
else if ([""] is T)
  val = prefs.getStringList(this._key);

but I cannot seem to compare T to an enum

Since all enums have the same methods I was hoping to sense them and handle them accordingly

Upvotes: 18

Views: 8717

Answers (4)

Oniya Daniel
Oniya Daniel

Reputation: 399

Convert the enum to a list and check if the list contains the value you intend to check.

enum ObjectType {
    TypeA, TypeB
}

// checking against enum values
print(ObjectType.values.contains(ObjectType.TypeA));        // true
print(ObjectType.values.contains(ObjectType.TypeB));        // true

// checking against string value
print(ObjectType.values.contains("ObjectType.TypeA"));    // false

// checking against boolean
print(ObjectType.values.contains(false));   // false

// checking against int
print(ObjectType.values.contains(234));       // false

// checking against double
print(ObjectType.values.contains(2.345));    // false

Upvotes: 2

Marcelo Glasberg
Marcelo Glasberg

Reputation: 30919

As of late 2021, all enums extend Enum

You can now finally use is Enum directly:

enum MyEnum {one, two, three}

var x = "a";
var y = MyEnum.one;

print(x is Enum); // false
print(y is Enum); // true

Which also means you can now create extensions for enums:

extension EnumExtension on Enum { ... }

Upvotes: 25

Michael K. Eidson
Michael K. Eidson

Reputation: 53

I don't have enough reputation to comment, or I would have added a statement to the answer by @Dmitry Puzak. In Dmitry's method, if data is passed as the String "String.3.0.1", for instance, the method will incorrectly identify the parameter as an enum. The flutter-provided method, describeEnum, attempts to check that it's parameter is an enum and throws an exception if it's not, but in my experience the exception isn't always thrown when the parameter to describeEnum is a String value containing dots.

For a project I'm working, I modified Dmitry's isEnum method to the following, adding the first if block inside the method:

bool isEnum(dynamic data) {
  if (data.runtimeType == "".runtimeType) {
    return false;
  }
  final split = data.toString().split('.');
  return split.length > 1 && split[0] == data.runtimeType.toString();
}

This makes sure to return false for any String parameter.

I can't offhand think of any other instance where the runtime type of a variable could possibly match the first part of the split done on the toString() return value.

Upvotes: 2

Dmytro Puzak
Dmytro Puzak

Reputation: 1491

If you need to check is dynamic value enum type. You can use the next approach. The main idea is quite similar to @Oniya Daniel's answer.

 enum Fruit {
   banana,
   apple,
   orange,
 }

Next method to check isEnum condition

 bool isEnum(dynamic data) {
   final split = data.toString().split('.');
   return split.length > 1 && split[0] == data.runtimeType.toString();
 }

Test result below

  test('check enum runtime', () {
    expect(isEnum(Fruit.banana), true);
    expect(isEnum(null), false);
    expect(isEnum(''), false);
    expect(isEnum('banana'), false);
  });

P.S.: to get enum String value good to use describeEnum(<enum_value>) from the package:flutter/foundation.dart

Upvotes: 13

Related Questions