I would know if we can declare a static var in a function, as we can do in JavaScript.
When I callback my function, my variable keep her last affectation.
Or
You can use Closures, in this example I have created a counter as follows:
void mian(){
var my_counter = counter(5);
for (var i = 0; i < 10; i++) {
print(my_counter());
}
}
Function counter(int n) {
var number = 0;
var func = () {
if (number < n) {
number++;
return number;
} else {
number = 1;
return number;
}
};
return func;
}
or also a Generator:
Iterable count(int n) sync* {
var number = 0;
while (number < n) {
yield number++;
}
}