class_members_ordering
Severity | Quick Fix | Options |
---|---|---|
Info | ❌ | ❌ |
Details
DO arrange class members in a consistent order.
Class members should be ordered in the following sequence:
- Public static fields
- Private static fields
- Public instance fields
- Private instance fields
- Public constructors
- Public named constructors
- Private constructors
- Private named constructors
- Public getters
- Private getters
- Public setters
- Private setters
- Public static methods
- Public instance methods
- Private static methods
- Private instance methods
For widgets, the order is slightly different:
- Public constructors
- Public named constructors
- Private constructors
- Private named constructors
- Public static fields
- Private static fields
- Public instance fields
- Private instance fields
- Public getters
- Private getters
- Public setters
- Private setters
- Public static methods
- Public instance methods
- Private static methods
- Private instance methods
Bad
class A {
void method() {}
int field;
A();
}
class AWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return const Placeholder();
}
final String field;
const AWidget({
super.key,
required this.field,
});
}
Good
class A {
int field;
A();
void method() {}
}
class AWidget extends StatelessWidget {
const AWidget({
super.key,
required this.field,
});
final String field;
@override
Widget build(BuildContext context) {
return const Placeholder();
}
}