0

I'm a beginner of flutter and dart. I'm teaching myself very hard these days, and I suddenly noticed there was awkward thing. I made a function called increase(), which was made to increase the integer input by one. But I found that didn't work.

I googled it a lot and I could found it's related to something called 'pointer'. But as you know I'm an appalling stupid, so couldn't get it well. Now, I just want to know how I increase the value 3 to 4. Thank you in advance. I paste my source code below.

void main() {
  int a = 3;
  increase(a);
  print(a);
}

increase(int x) {
  x++;
} 
Jinsub Kim
  • 133
  • 1
  • 2
  • 9

1 Answers1

1

that's because dart is pass by value and not by reference. There are ways around this but for your simple example you can do something like this

void main() {
  int a = 3;
  a = increase(a);
  print(a);
}

increase(int x) {
  return ++x;
}

also refer to this link for a similar question and other ways to go around it

Is there a way to pass a primitive parameter by reference in Dart?

laserany
  • 1,257
  • 1
  • 8
  • 17