Skip to main content

keep-state-below-its-widget

effort: 2m
has IDE fix
has auto-fix
pro+

Suggests placing the widget state right after the widget declaration.

Example

❌ Bad:

// LINT: Prefer keeping the widget state right after the widget declaration.
class MyWidget4State extends State<MyWidget4> {

Widget build(BuildContext context) {
return Container();
}
}

class MyWidget4 extends StatefulWidget {
const MyWidget4({super.key});


State<MyWidget4> createState() => MyWidget4State();
}

✅ Good:

class MyWidget4 extends StatefulWidget {
const MyWidget4({super.key});


State<MyWidget4> createState() => MyWidget4State();
}

class MyWidget4State extends State<MyWidget4> {

Widget build(BuildContext context) {
return Container();
}
}