Skip to main content
The @protected annotation is a metadata marker provided by the package:meta library that restricts the access scope of an instance member to the declaring library and classes that inherit from the defining class. It instructs the Dart analyzer to treat the annotated member as part of the class’s internal interface.

Prerequisite

Usage requires importing the meta package:
import 'package:meta/meta.dart';

Enforcement Mechanism

The @protected annotation functions exclusively as a static analysis hint.
  • Static Analysis: The Dart analyzer generates the invalid_use_of_protected_member warning if the member is accessed outside the permitted scope.
  • Runtime: The annotation has no effect on runtime behavior. Code that violates the visibility rules will compile and execute without exceptions, provided the member exists.

Visibility Rules

The analyzer enforces the following access constraints:
  1. Defining Library: Access is allowed anywhere within the same library (file) where the class is defined.
  2. Inheritance (extends / with): Access is allowed within instance methods of a class that extends or mixes in the defining class. Access is permitted on:
    • this (implicit or explicit).
    • super.
    • Another instance, provided the receiver’s static type is the enclosing subclass or a subtype of that subclass.
  3. Interfaces (implements): Access is disallowed for classes that only implement the defining class. Because implements treats the class purely as an interface contract, the implementing class does not inherit the implementation or the privilege to access protected members.

Syntax and Analysis Behavior

The following example demonstrates valid access via inheritance (including instance access) and invalid access via interfaces and unrelated classes. File: engine.dart
import 'package:meta/meta.dart';

class Engine {
  @protected
  void internalStart() {
    print("Engine started");
  }
}
File: usage.dart
import 'engine.dart';

// VALID: Subclass using 'extends'
class Car extends Engine {
  void drive() {
    // Allowed: Accessing inherited member on 'this'
    internalStart(); 
  }

  void sync(Car other) {
    // Allowed: Accessing protected member on another instance
    // because the receiver 'other' is of type 'Car' (the enclosing subclass).
    other.internalStart();
  }

  void invalidSelfRepair() {
    var newEngine = Engine();
    
    // STATIC WARNING: invalid_use_of_protected_member
    // Disallowed: The receiver 'newEngine' is the superclass type, not the subclass.
    newEngine.internalStart();
  }
}

// INVALID: Class using 'implements'
class MockEngine implements Engine {
  @override
  void internalStart() {
    // Implementation required because this is an interface contract.
  }

  void test() {
    // STATIC WARNING: invalid_use_of_protected_member
    // 'super' cannot be used because 'implements' does not inherit logic.
    // super.internalStart(); 
  }
}

// INVALID: Unrelated class
class Mechanic {
  void repair() {
    final car = Car();
    
    // STATIC WARNING: invalid_use_of_protected_member
    // Disallowed: Access outside the class hierarchy.
    car.internalStart(); 
  }
}
Master Dart with Deep Grasping Methodology!Learn More