Dart uses pass-by-value
for primitive types like int
or String
and uses pass-by-reference
for more complex data types like List
. Here the files are probably an object so it will take on the memory of the pointers.
This is a simple example for pass-by-value.
void main() {
void addOne(int x) {
x += 1;
print("x here is $x");
}
int x = 1;
addOne(x); //Output: "x here is 2"
print(x); //1
}
Here the value "x" is passed by value so, even if the function incremented x, the value in the main function will still remain 1.
example for pass-by-reference:
void main() {
void addList(List<int> x) {
x.add(12);
print("x here is $x");
}
List<int> x = [1, 2, 3, 4, 5];
addList(x); //x here is [1, 2, 3, 4, 5, 12]
print(x); //[1, 2, 3, 4, 5, 12]
}
Here the value of x is passed by reference so it would get updated in the main function as well
Passing the reference means to pass the memory location and not the actual value which is very efficient in case of big list or arrays or even complex objects. It can save much more memory than pass-by-value.
In your case the files
variable is a List of File
(List<File>) objects. That means that file1, file2, etc are File
objects and shall thus be passed-by-reference in dart