whenBoolSafe<T> function

T? whenBoolSafe<T>(
  1. bool value,
  2. List<Tuple2<bool, ValueGetter<T>>> conditionList,
  3. {ValueGetter<T>? defaultValue}
)

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.的错误警告;
如果conditionListTuple2.item1中有value的话,执行其对应的ValueGetter方法

example:

double degree = whenBoolSafe<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? whenBoolSafe<T>(bool value, List<Tuple2<bool, ValueGetter<T>>> conditionList,
    {ValueGetter<T>? defaultValue}) {
  for (var conditionTuple in conditionList) {
    if (conditionTuple.item1 == value) {
      return conditionTuple.item2();
    }
  }
  assert(
      defaultValue != null,
      "If you want to use the [whenBoolSafe] method, please make sure it can be executed in the [conditionList], or set the [defaultValue] function;"
      " of course, you can also directly use the [whenBool] method to get the return value that may be empty");
  return defaultValue!.call();
}