when<T> function

T? when<T>(
  1. Map<bool, ValueGetter<T>> conditionMap
)

English: The when function of the Kotlin version of the method switch.
As long as the first true is found in Map.keys of conditionMap, its corresponding ValueGetter method will be executed immediately and the relative value will be returned.
If it is not found, it will return null; if you need a default value, you can add a MapEntry with a key equal to true at the end of the Map

Special attention!!! Due to the characteristics of Map, you need to ensure that Map.keys in conditionMap does not have multiple true values. If you cannot guarantee it, it is recommended to use the whenTrue or whenBool method to avoid program problems

中文: 方法switch的kotlin版本的when函数.
只要在conditionMapMap.keys中发现第一个true,就会立刻执行其对应的ValueGetter方法,并返回相对的值.
如果没有找到的话,会返回null;如果需要默认值,可以在Map中最后加入一个key等于true的MapEntry

特别注意!!! 由于Map的特性,需要确保conditionMap中的Map.keys不会出现多个true的值。 如果不能保证,建议使用whenTrue或者whenBool方法,这样可以避免程序出现问题

example:

String? winner = when<String>({
  "Dart is Language".contains("UI"): () {
    return "Flutter";
  },
  "Flutter is UI Framework".contains("UI"): () {
    return "Flutter";
  },
});

Implementation

T? when<T>(Map<bool, ValueGetter<T>> conditionMap) {
  for (var element in conditionMap.entries) {
    if (element.key) {
      return element.value();
    }
  }
  return null;
}