Codes / Error Code 013
Codes / Error Code 013
Error Code 013
Overview
This error occurs when there is an attempt to modify a variable or object declared as immutable. Immutable values cannot be altered once initialized.
Details
-
Common Causes:
- Attempting to assign a new value to an
immutablevariable. - Modifying properties of an object that is declared as
immutable. - Passing an
immutableobject to a function expecting amutableparameter.
- Attempting to assign a new value to an
-
Example:
void main() {
immutable int x = 10;
x = 20; // Error: cannot modify immutable variable `x`
}
- Solution:
- Avoid assigning values to immutable variables after initialization:
void main() { immutable int x = 10; // x = 20; // Invalid } - If modification is necessary, use
constormutableinstead ofimmutable:void main() { int x = 10; // Mutable variable x = 20; // Valid } - When working with functions, ensure the correct parameter types:
void modifyValue(ref int x) { x = 42; } void main() { int y = 10; modifyValue(y); // Valid }
- Avoid assigning values to immutable variables after initialization: