> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Dart Index Assignment

The `[]=` operator, formally known as the index assignment operator, is a syntactic mechanism in Dart used to mutate the state of an object at a specified index or key. When the Dart compiler encounters this operator, it translates the assignment expression into an invocation of the underlying `operator []=` method.

```dart theme={"dark"}
void main() {
  List<String> items = ['a', 'b', 'c'];
  int index = 1;
  String value = 'z';
  
  // Standard assignment syntax
  items[index] = value; 
}
```

## Technical Characteristics

* **Arity and Parameters:** The `operator []=` method requires exactly two parameters. The first parameter represents the index or key (an expression evaluated to determine the target location), and the second parameter represents the value being assigned.
* **Type Flexibility:** The static types of both the index/key expression and the value are arbitrary and defined by the implementing class. For example, in a `List<T>`, the index expression must evaluate to an `int` and the value to `T`. In a `Map<K, V>`, the key expression evaluates to `K` and the value to `V`.
* **Return Type:** The return type of the `operator []=` method must be `void` (attempting to return a value results in a compile-time error). While modern Dart linting practices strongly recommend explicitly declaring the `void` keyword, the compiler does not strictly require it to be written. Note that the assignment expression itself evaluates to the assigned value (allowing chained assignments like `a[0] = b[0] = 5`), even though the underlying operator method returns nothing.

## Custom Implementation

Dart allows developers to overload the `[]=` operator within custom classes to define proprietary index assignment behavior. This is achieved by declaring a method named `operator []=`.

```dart theme={"dark"}
class Grid {
  final List<int> _data = List.filled(100, 0);

  // Overloading the index assignment operator
  // Parameter 1: The index/key expression (int)
  // Parameter 2: The value to assign (int)
  void operator []=(int index, int value) {
    if (index < 0 || index >= _data.length) {
      throw RangeError.index(index, _data);
    }
    _data[index] = value;
  }
}

void main() {
  Grid myGrid = Grid();
  myGrid[10] = 255; // Invokes the custom operator []=
}
```

## Null-Aware Assignment

The `[]=` operator interacts with Dart's null safety features. If the target object is potentially null, Dart utilizes a distinct null-aware index operator (`?[]`) to safely perform the assignment. This prevents runtime exceptions by short-circuiting the evaluation if the receiver is null.

```dart theme={"dark"}
void main() {
  List<int>? collection;
  int index = 0;
  int value = 42;

  // If 'collection' is null, the assignment is bypassed.
  // If 'collection' is non-null, operator []= is invoked.
  collection?[index] = value;
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Dart Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
