Metadata

Metadata provides additional information about code using annotations.

Built-in Annotations

class OldAPI {
  @deprecated
  void oldMethod() {
    print('This method is deprecated');
  }
  
  @Deprecated('Use newMethod() instead')
  void anotherOldMethod() {
    print('Use the new method');
  }
}

class MyClass {
  @override
  String toString() {
    return 'MyClass instance';
  }
}

Custom Annotations

class Todo {
  final String task;
  final String assignee;
  
  const Todo(this.task, this.assignee);
}

@Todo('Implement feature', 'Alice')
class FeatureClass {
  @Todo('Fix bug', 'Bob')
  void buggyMethod() {
    // Implementation
  }
}

Common Annotations

  • @override - Method overrides parent
  • @deprecated - Mark as deprecated
  • @pragma - Compiler hints
  • @immutable - Class should be immutable

Next Steps