'Is it necessary to use underscore (_) variable inside functions in Dart?

The variable/function name starts with underscore "_" means that it is a private variable/function in Dart language. It is well-known.

What if the underscore variable is used inside a function?

Is it necessary to use declare an underscored variable in a function(not in a class) to denote that the variable is only used in the function? or as it is self-evident that the declared variable can only be used in the function so that using underscore prefix is just a redundant thing? (or for the naming convention?) (when the function does not have any inner-function)

With underscore:

void sample() {
 var _something = getSomething();
 doSomethingWith(_something);
 //use _something
}

Without underscore:

void sample() {
 var something = getSomething();
 doSomethingWith(something);
 //use something
}

Plus, is there any performance-related difference between them?



Solution 1:[1]

Dai is right about almost everything but I want to correct him. Primitives (like int, bool, and num) are passed by value. Objects are passed by reference. So doSomethingWith will have reference to _something if _something is not a primitive But it doesn't change the answer. You don't need to underscore variables in methods.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Egor Polyakov