I have the following code:-

  switch (code) {
    case "BleDisconnectedException":
      throw BleDisconnectedException(msg, details);
    case "BleGattException":
      throw BleGattException(msg, details);
    case "BleGattCallbackTimeoutException":
      throw BleGattCallbackTimeoutException(msg, details);
    case "BleCharacteristicNotFoundException":
      throw BleCharacteristicNotFoundException(msg, details);
    case "BleGattCannotStartException":
      throw BleGattCannotStartException(msg, details);
    default:
      throw e;
  }

How do I make dart automatically do the switch-case, i.e. call the class constructor by its name, provided as a String?

1 Answers

0
Sebastian On

As I know in current Dart is not possible, but you can do something like this:

var factories = Map<String, Object Function()>{'Foo', () => new Foo(), 'Bar', () => new Bar()};

Object instance = factories['Foo']();