Reputation: 7176
Is it necessarily to mark varialbe as final inside function (method) in Dart?
Will it give any profit?
if (true) {
final diff = 32;
print(diff);
}
vs
if (true) {
var diff = 32;
print(diff);
}
Upvotes: 2
Views: 327
Reputation: 12169
I don't think it matters. Final is to initialize with a value that cannot be changed. From dart documentation:
"final" means single-assignment: a final variable or field must have an initializer. Once assigned a value, a final variable's value cannot be changed.
Since diff variable's storage only exists within the scope of the function, when the function returns, the variable no longer exists. So unless you are concerned about preventing the value from changing in the function, it does not matter otherwise.
Upvotes: 2