final
doesn't mean deep final
.
The list
variable references still the same list instance even when you modify the lists content. Any mutable instance can be modified even when it was assigned to a final variable.
Imagine
void main() {
var l = [1,2,3];
final List<int> list = l;
}
Now you wouldn't be able to modify the items in the list referenced by l
because the list is also assigned to the final field list
(both list
and l
reference the same list). That doesn't make sense.
What you can do to make the list immutable is
final List<int> list = const[1,2,3];
Now you can't assign another list to list
and you can't modify the contents of the list referenced by list
.
An alternative way
import 'dart:collection'
...
var l = [1,2,3];
final List<int> list = UnmodifiablyListView(l);
Now you can't modify list
or the contents of the list referenced by list
but you can modify the contents referenced by l
(list
would reflect the changes made to l
).
If you loose the reference to l
you have no way of modifying the contents.
var l = [1,2,3];
final List<int> list = UnmodifiablyListView(l);
l = null;
final
is nice when you for example want to ensure that the list
field never is set to null
.
class MyModel {
final list = [];
}
The list
field is public but nobody can set list
to for example null
.
var model = new MyModel();
...
model.list.forEach(print);
will never fail with an exception like null doesn't have a method 'forEach'
.
This is similar but more concise than
class MyModel {
var _list = [];
List get list => _list;
}
void main() { final List<int> list = [1,2,3]; List<int> l=list; l=[10,9,8]; }
then atl=[10,9,8]
a deep copy oflist
will be created; One can see that clearly in the debugger: at line 2 id's ofl
andlist
will be the same, i.e. they refer to the same object in memory, while at line 3, these references will be different. I agree, your first example does not make any sense, but the bahaviour I described does not seem to be very meaningful either, i.e. why at all have finals if there is such a simple way to re-assign them? Prev comment didn't come out right. – Yellowtail