alexrintt
@alexrinttAndroid/Flutter.

I've always wanted a when statement when working with Flutter/Dart. Most of the time UI is not a if else but a switch!

So instead of writing:

late Color color;

if (isLoading) {
  color = Colors.yellow;
} else if (isOnline) {
  color = Colors.green;
} else {
  color = Colors.red;
}

return ColoredBox(color: color);

You can now simply inline this:

return ColoredBox(
  color: switch (null) {
      _ when isLoading => Colors.yellow,
      _ when isOnline => Colors.green,
      _ => Colors.red,
    },
);

Other posts

you reached the end