Search code examples
flutterconditional-operator

Dart Flutter: Set two variable at the same time with ternary operator


As the title states, I'd like to do something like this in dart/flutter:

bool someBool = true;
double pad = 10.5;
double left, right = someBool ? (padd / 4, padd) : (padd, padd / 4);

Is there a way to achieve this as a one liner in dart, or do I really have to resort to the following:

bool someBool = true;
double pad = 10.5;
double left = someBool ? padd / 4 : padd;
double right = someBool ? padd : padd / 4;

Thanks!


Solution

  • You will need to use two ternary operators:

    double left = someBool ? pad / 4 : pad, right = someBool ? pad : pad / 4;