Home > Enterprise >  when case from kotlin. is there similar function in flutter?
when case from kotlin. is there similar function in flutter?

Time:08-15

Here is the example from kotlin

binding.orderStatusTextView.text = when (status) {
            2 -> "bad"
            3 -> "not bad"
            4 -> "good"
            5 -> "excellent"
            else -> "New"
    }

CodePudding user response:

As @Yeasin Sheikh suggested you can use switch case like this.

String getStatus(int code){
    switch(code){
        case 2: return 'bad';
        case 3: return 'Not bad';
        case 4: return 'good';
        case 5: return 'Excellent';
        default: return "";
    }
}

_textController.text = getStatus(status);

CodePudding user response:

Using switch inside an anonymous function matches closely with Kotlin's when clause:

    final statusValue = 4;

    final value = (status) {
      switch (status) {
        case 2:
          return 'bad';
        case 3:
          return 'Not bad';
        case 4:
          return 'good';
        case 5:
          return 'Excellent';
        default:
          return "";
      }
    }(statusValue);

    print(value);
  • Related