whenStringSafe<T> function

T whenStringSafe<T>(
  1. String value,
  2. Map<String, ValueGetter<T>> conditionMap, {
  3. ValueGetter<T>? defaultValue,
})

English: Used to replace the switch method, because in some scenarios, an error warning of Case expressions must be constant;
If there is value in Map.keys of conditionMap, execute its corresponding ValueGetter method

中文: 用于取代switch方法,因为有些场景使用switch会出现Case expressions must be constant.的错误警告;
如果conditionMapMap.keys中有value的话,执行其对应的ValueGetter方法

example:

int index = whenString<int>("banana🍌", {
  "water" + "melon": () {
    return 1;
  },
  "apple": () {
    return 2;
  },
  "orange": () {
    return 3 ;
  },
  "banana" + "🍌": () {
    return 4;
  },
  "grape": () {
    return 5;
  },
});

Implementation

T whenStringSafe<T>(String value, Map<String, ValueGetter<T>> conditionMap,
    {ValueGetter<T>? defaultValue}) {
  return (conditionMap[value] ?? defaultValue)!.call();
}