Language: English | 中文简体

switch_when

A function library that provides a more advanced switch, does not restrict Case expressions to be constants, similar to kotlin's when method.

Getting Started

Add dependency

dependencies:
  switch_when: ^0.0.1 #please use the latest version on pub

Use Library

  1. import package Where you need to use the Library library, import the package
import 'package:switch_when/switch_when.dart';
  1. Use functions Different methods are used according to different situations. For general situations, you can use the when method or whenValue method. If you want the type of the returned result to be null safe, you can use the whenSafe method or whenValueSafe method.
  @override
  void initState() {
    _curtainAnimationImage = TweenImageWidget(
      ImagesEntry(1, 20, "equipmentcontrol_pic_curtain%s".toAssetImg()),
      durationMilliseconds: 5000,
      repeat: false,
    );
    super.initState();
  }

Functions:

//Not Safe
T? whenString<T>(String value, Map<String, ValueGetter<T>> conditionMap)
T? whenInt<T>(int value, Map<int, ValueGetter<T>> conditionMap)
T? whenDouble<T>(double value, Map<double, ValueGetter<T>> conditionMap) 
T? whenBool<T>(bool value, List<Tuple2<bool, ValueGetter<T>>> conditionList)
T? whenValue<V, T>(V value, Map<V, ValueGetter<T>> conditionMap)
T? when<T>(Map<bool, ValueGetter<T>> conditionMap)
T? whenTrue<T>(Map<ValueGetter<bool>, ValueGetter<T>> conditionMap)

//Safe
T whenStringSafe<T>(String value, Map<String, ValueGetter<T>> conditionMap, {ValueGetter<T>? defaultValue}) 
T whenIntSafe<T>(int value, Map<int, ValueGetter<T>> conditionMap, {ValueGetter<T>? defaultValue})
T whenDoubleSafe<T>(double value, Map<double, ValueGetter<T>> conditionMap, {ValueGetter<T>? defaultValue}) 
T? whenBoolSafe<T>(bool value, List<Tuple2<bool, ValueGetter<T>>> conditionList, {ValueGetter<T>? defaultValue}) 
T whenValueSafe<V, T>(V value, Map<V, ValueGetter<T>> conditionMap, {ValueGetter<T>? defaultValue}) 
T whenSafe<T>(Map<bool, ValueGetter<T>> conditionMap, {ValueGetter<T>? defaultValue})
T whenTrueSafe<T>(Map<ValueGetter<bool>, ValueGetter<T>> conditionMap, {ValueGetter<T>? defaultValue}) 
T? whenString

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

example:

 int? index = whenString<int>("banana🍌", {
   "water" + "melon": () {
     return 1;
   },
   "apple": () {
     return 2;
   },
   "orange": () {
     return 3 ;
   },
   "banana" + "🍌": () {
     return 4;
   },
   "grape": () {
     return 5;
   },
 });
T? whenInt

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

example:

 String? status = whenInt<String>(1, {
   1: () {
     return "good";
   },
   1 + 1: () {
     return "nice";
   },
   int.parse("3"): () {
     return "better";
   },
 });
T? whenDouble

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

example:

 String? status = whenDouble<String>(2.0, {
   0.1: () {
     return "good";
   },
   1.0 + 1: () {
     return "nice";
   },
   double.parse("3.2"): () {
     return "better";
   },
 });
T? whenBool

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

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;
T? whenValue

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

example:

 String? kind = whenValue<List, String>(
   ["apple", "orange"],
   {
     ["cat", "dog"]: () {
       return "pets";
     },
     ["apple", "orange"]: () {
       return "fruits";
     },
     ["red", "white", "black"]: () {
       return "colors";
     },
   },
 );
T? when

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

example:

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

The when function of the Kotlin version of the method [switch],its conditional expression will be calculated。.
As long as [conditionMap] to [Map.keys] appears first execution result is true, it will immediately perform corresponding [ValueGetter] method, and an opposite return value.
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

example:

String? something = whenTrue<String>({
      () {
    if (1 + 100 * 1000 < 2000) {
      return false;
    } else if ("Who is my lovely baby?".length > 10) {
      return true;
    } else {
      return false;
    }
  }: () {
    return "Test OK";
  },
      () {
    return int.tryParse("3.14*") != null;
  }: () {
    return "PI get";
  }
});

Example Demo:

  testWhenValue() {
    String? kind = whenValue<List, String>(
      ["apple", "orange"],
      {
        ["cat", "dog"]: () {
          return "pets";
        },
        ["apple", "orange"]: () {
          return "fruits";
        },
        ["red", "white", "black"]: () {
          return "colors";
        },
      },
    );
    return kind;
  }

  testWhenString() {
    int howManyFruits = 2;
    int? index = whenString<int>("banana🍌", {
      "water" + "melon".more(): () {
        return 1;
      },
      "apple".more(): () {
        return 2;
      },
      howManyFruits <= 1 ? "orange" : "oranges": () {
        return 3;
      },
      "banana" + "🍌": () {
        return 4;
      },
      "grape": () {
        return 5;
      },
    });
    return index;
  }

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

  testWhenDoubleSafe() {
    String status = whenDoubleSafe<String>(
      2.1,
      {
        0.1: () {
          return "good";
        },
        1.0 + 1: () {
          return "nice";
        },
        double.parse("3.2"): () {
          return "better";
        },
      },
      defaultValue: () => "other",
    );
    expect(status, "other");
  };

Demo Pictrue

demo

Libraries

switch_when