ClassDeclaration class abstract
Represents a reflected Dart class and all of its metadata, including fields, methods, constructors, superclasses, mixins, interfaces, and declaration-level modifiers (abstract, sealed, etc.).
This interface combines both SourceDeclaration and TypeDeclaration, allowing it to be used both as a type descriptor and a declaration node.
Use this class to introspect:
- Class members: fields, methods, constructors
- Generic type parameters
- Supertype, mixins, and implemented interfaces
- Modifiers like
abstract
,sealed
,base
, etc. - Runtime instantiation via
newInstance()
Example
final type = reflector.reflectType(MyService).asClassType();
print('Class: ${type?.getName()}');
for (final method in type!.getMethods()) {
print('Method: ${method.getName()}');
}
final instance = type.newInstance({'message': 'Hello'});
- Inheritance
-
- Object
- Declaration
- EntityDeclaration
- TypeDeclaration
- ClassDeclaration
- Implemented types
- Available extensions
Constructors
- ClassDeclaration()
-
Represents a reflected Dart class and all of its metadata, including
fields, methods, constructors, superclasses, mixins, interfaces, and
declaration-level modifiers (abstract, sealed, etc.).
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
asClass(
) → ClassDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Safely casts to ClassDeclaration if possible. -
asEnum(
) → EnumDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Safely casts to EnumDeclaration if possible. -
asMixin(
) → MixinDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Safely casts to MixinDeclaration if possible. -
asRecord(
) → RecordDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Safely casts to RecordDeclaration if possible. -
asTypedef(
) → TypedefDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Safely casts to TypedefDeclaration if possible. -
asTypeVariable(
) → TypeVariableDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Safely casts to TypeVariableDeclaration if possible. -
getAllMethodsInHierarchy(
) → List< MethodDeclaration> -
Available on ClassDeclaration, provided by the ClassHierarchyExtensions extension
Get all methods from this class and its hierarchy -
getAnnotations(
) → List< AnnotationDeclaration> -
Returns all annotations applied to this declaration.
inherited
-
getConstructors(
) → List< ConstructorDeclaration> - Returns all constructors declared in this class.
-
getDartType(
) → DartType? -
The analyzer DartType of the entity for enhanced type operations.
inherited
-
getDebugIdentifier(
) → String -
The debug identifier for the entity.
inherited
-
getDeclaration(
) → SourceDeclaration? -
Available on TypeDeclaration, provided by the TypeDeclarationExtension extension
Resolves the most specific declaration type. -
getElement(
) → Element? -
The analyzer element associated with this declaration.
inherited
-
getFields(
) → List< FieldDeclaration> - Returns all fields declared in this class (excluding inherited fields).
-
getInterfaces(
) → List< LinkDeclaration> -
Returns the list of interfaces this type implements.
inherited
-
getIsAbstract(
) → bool -
Returns
true
if this class is markedabstract
. -
getIsBase(
) → bool -
Returns
true
if this class is markedbase
. -
getIsFinal(
) → bool -
Returns
true
if this class is markedfinal
. -
getIsInterface(
) → bool -
Returns
true
if this class is declared as aninterface
. -
getIsMixin(
) → bool -
Returns
true
if this is amixin
declaration or a mixin application. -
getIsNullable(
) → bool -
Returns
true
if the type is nullable, such as'String?'
or'int?'
.inherited -
getIsPublic(
) → bool -
Checks if this declaration is a public declaration.
inherited
-
getIsRecord(
) → bool -
Returns
true
if this class is arecord class
(e.g., a class wrapping a record). -
getIsSealed(
) → bool -
Returns
true
if this class is markedsealed
. -
getIsSynthetic(
) → bool -
Checks if a declaration is a synthetic declaration.
inherited
-
getKind(
) → TypeKind -
Returns the TypeKind of this type, indicating whether it is a class, enum, mixin, etc.
inherited
-
getMembers(
) → List< MemberDeclaration> - Returns all members declared in this class, including fields, methods, and constructors.
-
getMethods(
) → List< MethodDeclaration> - Returns all methods declared in this class (excluding inherited methods).
-
getMixins(
) → List< LinkDeclaration> -
Returns the list of mixin identities that are applied to this type.
inherited
-
getName(
) → String -
Gets the name of the declared element.
inherited
-
getPackageUri(
) → String -
Returns the package URI where this type is declared.
inherited
-
getParentLibrary(
) → LibraryDeclaration -
Returns the LibraryDeclaration in which this declaration is defined.
inherited
-
getQualifiedName(
) → String -
Returns the fully qualified name of this type.
inherited
-
getRecords(
) → List< RecordDeclaration> - Returns all records declared in this class (excluding inherited records).
-
getSimpleName(
) → String -
Returns the simple name of the type without the package or library URI.
inherited
-
getSourceLocation(
) → Uri? -
Returns the source code location (e.g., file path or URI) where this declaration is defined,
or
null
if not available in the current reflection context.inherited -
getSuperClass(
) → LinkDeclaration? -
Returns the direct superclass of this type.
inherited
-
getType(
) → Type -
Gets the runtime type of the declared element.
inherited
-
getTypeArguments(
) → List< LinkDeclaration> -
Returns the list of type arguments for generic types.
inherited
-
hasAnalyzerSupport(
) → bool -
Returns true if this declaration has analyzer information available.
inherited
-
isAssignableFrom(
TypeDeclaration other) → bool -
Type Assignability Table:
inherited
-
isAssignableTo(
TypeDeclaration target) → bool -
Type Assignability Table:
inherited
-
isGeneric(
) → bool -
Check if this is a generic type.
inherited
-
newInstance(
Map< String, dynamic> arguments) → dynamic - Instantiates this class using the default (unnamed) constructor.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, Object> -
Returns a JSON representation of this entity.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited