CreationLocation class final

Holds the source code location where an object was created, when its class was annotated with @pragma('track-creation-locations').

When a class definition is annotated with @pragma('track-creation-locations'), the Dart compiler injects the call-site location into any invocation of that class's or any subclass's constructors and stores it in the created object.

The location of such an object can be read by calling CreationLocation.of with the object as the argument.

Example

import 'dart:developer';

// Marks this and any subclass to have their constructor call-sites tracked.
@pragma('track-creation-locations')
class TargetClass {
  TargetClass();
}

void main() {
  // The source-code location of this constructor call is injected into the object.
  final instance = TargetClass();

  final location = CreationLocation.of(instance);
  print(location); // Will print the current file path, line 11, column 20
}

Limitations

The compiler transformation relies on injecting a named parameter into the target class's constructors. Since Dart semantics do not permit a function to have both optional positional parameters and named parameters simultaneously, this transformation will silently skip any constructor that declares optional positional parameters. Calling CreationLocation.of on an object whose constructor was skipped, will return null.

Properties

column int
1-based column number.
final
file String
File path of the location.
final
hashCode int
The hash code for this object.
no setterinherited
line int
1-based line number.
final
name String?
Optional name of the parameter or function at this location.
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJsonMap() Map<String, Object?>
JSON representation of this location.
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

of(Object? object) CreationLocation?
Returns the creation location of object.