Codes / Error Code 005
Codes / Error Code 005
Error Code 005
Overview
This error occurs when you try to assign a new value to a variable or object marked as immutable. Immutable variables cannot be modified after their initial assignment.
Details
-
Common Causes:
- Attempting to modify an
immutablevariable. - Reassigning a value to a property of an immutable object.
- Attempting to modify an
-
Example:
void main() {
immutable int x = 5;
x = 10; // Error: cannot modify immutable variable `x`
}
- Solution:
- Use
mutableorconstinstead ofimmutableif modification is necessary:void main() { int x = 5; x = 10; // Valid } - Reassign values to new variables if immutability must be preserved:
void main() { immutable int x = 5; int y = x + 5; // Assign to a new variable }
- Use