2

I have a for loop in my Flutter project similar to this (simplified):

for(int i=0; i<aVariableDeclaredBefore; i++) Padding(....

Here I need another operation after i++ run as this one anotherVariable--. I can't do it in {} braces because loop used for Widget manipulation. There is no curly braces there.

I need something like this ((i++)&&(anotherVariable--)) but how ? if it is possible

1
  • 1
    Alternatively consider if you need anotherVariable at all. If you're always incrementing i and always decrementing anotherVariable, on each iteration you could derive the intended value for anotherVariable from i. Commented Jan 23, 2023 at 5:07

1 Answer 1

4

Use the comma operator:

void main() {
  for (var i = 0, j = 10; i < 10; i++, j++) {
    print('i=$i, j=$j');
  }
}

Output:

i=0, j=10
i=1, j=11
i=2, j=12
i=3, j=13
i=4, j=14
i=5, j=15
i=6, j=16
i=7, j=17
i=8, j=18
i=9, j=19
Sign up to request clarification or add additional context in comments.

1 Comment

You wrote: Use the comma operator. You're not right. The comma is not an operator. This is the expression separator in the list of expressions. You shouldn't mislead people. Dart does not and never will have the , operator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.