extension_ type_ declares_ instance_ field
Extension types can't declare instance fields.
Description
#The analyzer produces this diagnostic when there's a field declaration in the body of an extension type declaration.
Example
#
The following code produces this diagnostic because the extension type
E
declares a field named
f
:
extension type E(int i) {
final int f = 0;
}
Common fixes
#If you don't need the field, then remove it or replace it with a getter and/or setter:
extension type E(int i) {
int get f => 0;
}
If you need the field, then convert the extension type into a class:
class E {
final int i;
final int f = 0;
E(this.i);
}
Unless stated otherwise, the documentation on this site reflects Dart 3.9.2. Page last updated on 2025-9-1. View source or report an issue.