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

I would like to pass a primitive (int, bool, ...) by reference. I found a discussion about it (paragraph "Passing value types by reference") here: value types in Dart, but I still wonder if there is a way to do it in Dart (except using an object wrapper) ? Any development ?

41854 次浏览

No, wrappers are the only way.

They are passed by reference. It just doesn't matter because the "primitive" types don't have methods to change their internal value.

Correct me if I'm wrong, but maybe you are misunderstanding what "passing by reference" means? I'm assuming you want to do something like param1 = 10 and want this value to still be 10 when you return from your method. But references aren't pointers. When you assign the parameter a new value (with = operator), this change won't be reflected in the calling method. This is still true with non-primitive types (classes).

Example:

class Test {
int val;
Test(this.val);
}


void main() {
Test t = new Test(1);
fn1(t);
print(t.val); // 2
fn2(t);
print(t.val); // still 2, because "t" has been assigned a new instance in fn2()
}


void fn1(Test t) {
print(t.val); // 1
t.val = 2;
}


void fn2(Test t) {
t = new Test(10);
print(t.val); // 10
}

EDIT I tried to make my answer more clear, based on the comments, but somehow I can't seem to phrase it right without causing more confusion. Basically, when someone coming from Java says "parameters are passed by reference", they mean what a C/C++ developer would mean by saying "parameters are passed as pointers".

The Dart language does not support this and I doubt it ever will, but the future will tell.

Primitives will be passed by value, and as already mentioned here, the only way to 'pass primitives by reference' is by wrapping them like:

class PrimitiveWrapper {
var value;
PrimitiveWrapper(this.value);
}


void alter(PrimitiveWrapper data) {
data.value++;
}


main() {
var data = new PrimitiveWrapper(5);
print(data.value); // 5
alter(data);
print(data.value); // 6
}

If you don't want to do that, then you need to find another way around your problem.

One case where I see people needing to pass by reference is that they have some sort of value they want to pass to functions in a class:

class Foo {
void doFoo() {
var i = 0;
...
doBar(i); // We want to alter i in doBar().
...
i++;
}


void doBar(i) {
i++;
}
}

In this case you could just make i a class member instead.

As dart is compiled into JavaScript, I tried something that works for JS, and guess what!? It worked for dart!

Basically, what you can do is put your value inside an object, and then any changes made on that field value inside that function will change the value outside that function as well.

Code (You can run this on dartpad.dev)

main() {
var a = {"b": false};
  

print("Before passing: " + a["b"].toString());
trial(a);
print("After passing: " + a["b"].toString());
}


trial(param) {
param["b"] = true;
}

Output

Before passing: false
After passing: true

You can use ValueNotifier

And, you can pass it as ValueListenable to classes or methods that needs to know up-to-date value, but should not edit it:

class Owner {
final theValue = ValueNotifier(true);
final user = User(theValue);
...
}


class User {
final ValueListeneble<bool> theValue;
  

User(this.theValue);


...
}




It provides more functionality than actually needed, but solves the problem.

One of the way to pass the variables by reference by using the values in List. As arrays or lists are Pass by reference by default.

void main() {
List<String> name=['ali' ,'fana'];
updatename(name);
print(name);
}
updatename(List<String> name){
name[0]='gufran';
}

Try this one, This one of the simplest way to pass by reference.