What if i assign a const to a var?
I tried to run this code
var a = const [1, 2, 3];
a.add(10);
print(a);
but I am having an error. As I knew that assigning and const to a variable doesn't make it a const. So why is this error occurring?
Unhandled exception: Unsupported operation: Cannot add to an unmodifiable list
Variables in Dart is always references and the type of the variable does not change the state of the object it points to.
So in your case, you have declared a const
list which means it is a compile-constant defined list and is therefore implicit unmodifiable.
You now point to this list by using a normal variable. But the type of the variable does not change the fact that your List
is created as const
from the beginning.