whenIntSafe<T> function
T
whenIntSafe<T>(
- int value,
- Map<
int, ValueGetter< conditionMap, {T> > - 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.的错误警告;
如果conditionMap
的Map.keys中有value
的话,执行其对应的ValueGetter方法
example:
String status = whenInt<String>(1, {
1: () {
return "good";
},
1 + 1: () {
return "nice";
},
int.parse("3"): () {
return "better";
},
});
Implementation
T whenIntSafe<T>(int value, Map<int, ValueGetter<T>> conditionMap,
{ValueGetter<T>? defaultValue}) {
return (conditionMap[value] ?? defaultValue)!.call();
}