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, ie call the class constructor by its name, provided as a String
?
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']();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.