@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 themeta package:
Enforcement Mechanism
The@protected annotation functions exclusively as a static analysis hint.
- Static Analysis: The Dart analyzer generates the
invalid_use_of_protected_memberwarning 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:- Defining Library: Access is allowed anywhere within the same library (file) where the class is defined.
- 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.
- Interfaces (
implements): Access is disallowed for classes that only implement the defining class. Becauseimplementstreats 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
usage.dart
Master Dart with Deep Grasping Methodology!Learn More





