whenValueSafe<V, T> function
T
whenValueSafe<V, T>(
- V value,
- Map<
V, ValueGetter< conditionMap, {T> > - ValueGetter<
T> ? defaultValue,
English: The super evolution version💖💖💖 of switch
method, all basic types of values can be compared, including List, Map, Set, and Iterable.
All need do is value
in Map.keys of conditionMap
, its corresponding ValueGetter method will be executed
中文: 方法switch
的超级进化版本💖💖💖,所有基本类型的value都可以比较,包括List,Map,Set,以及Iterable。
只要conditionMap
的Map.keys中有value
的话,就会执行其对应的ValueGetter方法
example:
String kind = whenValue<List, String>(
["apple", "orange"],
{
["cat", "dog"]: () {
return "pets";
},
["apple", "orange"]: () {
return "fruits";
},
["red", "white", "black"]: () {
return "colors";
},
},
);
Implementation
T whenValueSafe<V, T>(V value, Map<V, ValueGetter<T>> conditionMap,
{ValueGetter<T>? defaultValue}) {
return (conditionMap.entries
.firstOrNullWhere((element) =>
const DeepCollectionEquality().equals(element.key, value))
?.value ??
defaultValue)!
.call();
}