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 immutable variable.
    • Reassigning a value to a property of an immutable object.
  • Example:

void main() {
    immutable int x = 5;
    x = 10; // Error: cannot modify immutable variable `x`
}
  • Solution:
    • Use mutable or const instead of immutable if 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
      }