Skip to main content

avoid-stateless-widget-initialized-fields

added in: 1.5.0
Pro+

Warns when a StatelessWidget has an initialized final field.

Non-static initialized fields are usually a sign of a state. Consider converting the widget to StatefulWidget.

note

You can fix this rule's issues using the "Convert to StatefulWidget" assist.

Example

❌ Bad:

class AnotherWidget extends StatelessWidget {
// LINT: Avoid initialized fields in a 'StatelessWidget'. Try converting this widget to a 'StatefulWidget' instead.
final initialized = <String>{};

// LINT: Avoid initialized fields in a 'StatelessWidget'. Try converting this widget to a 'StatefulWidget' instead.
late final someField = <String>{};
}

✅ Good:

class AnotherWidget extends StatelessWidget {
static final initialized = <String>{};

final someField;

const AnotherWidget(this.someField);
}

// Or

class _AnotherWidgetState extends State<AnotherWidget> {
final initialized = <String>{};

late final someField = <String>{};
}