whenBool<T> function
English: Used to replace the switch method, because some scenes use switch
to cause an error warning of Case expressions must be constant.
If there is value
in Tuple2.item1
of conditionList
, execute its corresponding ValueGetter method
中文: 用于取代switch方法,因为有些场景使用switch
会出现Case expressions must be constant.的错误警告;
如果conditionList
的Tuple2.item1
中有value
的话,执行其对应的ValueGetter方法
example:
double? degree = whenBool<double>(false, [
Tuple2(
"is Long String".length > 10,
() {
return 0.0;
},
),
Tuple2(
100 / 10 == 0,
() {
return 1.0;
},
),
Tuple2(
"apple".contains("a"),
() {
return 2.0;
},
),
]);
return degree;
Implementation
T? whenBool<T>(bool value, List<Tuple2<bool, ValueGetter<T>>> conditionList) {
for (var conditionTuple in conditionList) {
if (conditionTuple.item1 == value) {
return conditionTuple.item2();
}
}
return null;
}