1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Switch文で複数の値を同じcaseにしたい【Dart】

Posted at

始めに

Flutterで開発している際にSwitch文を使って返すWidgetを捌きたいケースが出てきた
その時にインデックスの0~2までは同じ値を返したい。みたいなのをどうやるのかが中々分からなかったので備忘録

どうやるの?

こうやる

    switch (_index) {
      case 0:
      case 1:
      case 2:
        return Text('index is 0 to 2');
      case: 3:
        return Text('index is 3');   
      default:
        return Text('default');

判定したい値を,とか||で繋ぐのかと思いきや、caseを羅列してbreak;を書かないという感じで書く

まあそれは通るよねという感じでした
もし何かもっとスマートな書き方があったら是非教えていただきたいです

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?