invalid_reference_to_this
Invalid reference to 'this' expression.
Description
#The analyzer produces this diagnostic when this
is used outside of an instance method or a generative constructor. The reserved word this
is only defined in the context of an instance method, a generative constructor, or the initializer of a late instance field declaration.
Example
#The following code produces this diagnostic because v
is a top-level variable:
dart
C f() => this;
class C {}
Common fixes
#Use a variable of the appropriate type in place of this
, declaring it if necessary:
dart
C f(C c) => c;
class C {}
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-05-08. View source or report an issue.