You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Consistently structured class declarations are often regarded as not important but they sure improve code readability and a definite must-have for teams.
The most comprehensive guide on this topic is probably Style guide for Flutter repo. The suggested default order is quite detailed and potentially could be implemented as a conventional Flutter Style rule.
However not everyone may find such a structure appropriate for their project. For example, I find it odd that private methods used in build should be listed before it. I'm new to Dart and honestly don't know if the Linter supports configurable rules, but in the ideal world it could look something like this:
# Uses flutter repo style by default
- class_members_ordering# Custom order that potentially could be specified separately for different paths,# e.g. `lib/models`, `lib/widgets` etc.class_members_ordering:
- constructors
- factories
- instance_variables# etc.
The linter doesn't support configurable rules. We've talked about adding support before but haven't yet done so.
One of the reasons we don't have a lint to enforce ordering is the lack of consensus as to what the ordering ought to be. Without some way for different teams to define their own preference and a lack of consensus within the community it would be difficult to know what order to support.
Within IntelliJ (I'm not sure about VS Code) there is a "Sort Members in Dart File" command. The team that works on the Dart analyzer and analysis server use that command to enforce an order on the code. It's a bit simpler, grouping declarations by kind and then by name. I mention it because there is at least some level of tooling support for it which might be useful for you.
Thanks @jibiel for the request and Brian for your response! FWIW, here's the issue tracking configurability, in general, as a feature: #57673. There's some additional context there. Feel free to chime in! (You may also find this long conversation about support for custom rules interesting: #57588.)
As for the request in general, I'd personally very much like a sort-order lint. For my (personal) purposes, one that simply agreed with the order enforced by the "Sort Members in Dart File" command would be a great step. (It's interesting to note that we have an extra step of analysis to enforce this in the analyzer codebase. See: verify_sorted_test. With a lint we could make this go away and it would be easier for others to benefit.)
Aside: I just yesterday introduced some unwanted churn in the linter codebase that would have been avoided if we were enforcing sorting on commit (see: dart-archive/linter#2362).
@bwilkerson Turns out we also have "Dart: Sort Members" within VS Code via Dart extension. But it produces strange results:
Placing static constants/variables and final fields before a constructor (that's actually how I've would've organized it before I was introduced to the Flutter repo style guide) but with what looks like random blank lines that mess grouped static/final lines.
Removing blank lines between constructors and factories.
Removing blank lines between import groups (I like to separate external and internal imports).
Similar to directives_ordering, there should be a linter rule to sort members according to the Dart Style Guide and one according to the Flutter Style Guide.
It doesn't need any configuration since that would go against the reasons why linter rules aren't configurable in the first place. Sure, I disagree with lots of the reasoning that both styles have for their chosen ordering, but their goal isn't to satisfy me personally so that's fine. There's no consensus on spaces vs tabs but dartfmt still picks one and forces it on everyone and that's fine.
The current patchwork of using an external extension (even if it's first-party) to do it isn't viable if it's something that a team wants to be enforced via CI.
I think this issue should be focused specifically on member_ordering & flutter_member_ordering without considering potential configuration :
linter:
rules:
member_ordering: true
linter:
rules:
flutter_member_ordering: true
Reacted by Marcus Wichelmann, Vincent Hamp and Alexey Zemtsov
Are there any updates or plans regarding the implementation of sorting? For further inspiration, you can refer to the documentation on sorting components in the eslint-plugin-react repository on GitHub: eslint-plugin-react - sort-comp.md or TypeScript member-ordering
No updates but this is still something I'd very much like to see happen. As I mentioned in #58283, the absence of this in lint form leads to ad hoc solutions and lots of needless churn. I do wish we could converge on one agreed ordering (and admit that's part of why I haven't pushed harder on this) but could get over that I guess.
@goderbauer: I'm curious, from the flutter perspective how valued this enforcement would be?
Reacted by Alexey Zemtsov, Bruno Nahorny and Romulo Pulcinelli Benedetti
So far, this hasn't been a huge problem in the Flutter repository. I can't remember the last time I had to ask in a code review to change the order to match Flutter's style. So, while this would be nice to have - and it would be interesting to see where we are currently breaking our own rules - I wouldn't consider it very high priority for us.
I would like to bring to your attention that according to the Flutter Style Guide, it is recommended to have the mentioned rule in place.
If there’s a clear lifecycle, then the order in which methods get invoked would be useful, for example an initState method coming before dispose. This helps readers because the code is in chronological order, so they can see variables get initialized before they are used, for instance. Fields should come before the methods that manipulate them, if they are specific to a particular group of methods.
If there are no current priorities or plans to incorporate this rule, it would be helpful to officially close the issue with a clear explanation. This will ensure that there are no false expectations or lingering anticipation regarding the implementation of the rule.
I'm not in a rush to close this one. I do think it will happen, I just don't know when. It may get easier to implement soon too as we're looking at re-structuring the linter sources to live closer to the analyzer bits they're built on.
We won't be implementing this as a warning or a lint rule in the Dart analyzer. We now have a high bar for implementing and maintaining built-in static analysis rules, and this rule supports a bespoke style. As of Dart 3.10 (Flutter 3.38), a new analyzer plugins system is available, and the requested static analysis here could be implemented as an analyzer plugin.
@srawlins does that relate to a customisable lint/ordering, or a lint for sorting in general?
I ask because there's a similar request for a fix at #36152 so if the idea is that a plugin should be used to enforce any sorting, probably the same applies there. However, server already has code for sorting (which is accessible as a command to the IDEs), and duplicating that in a plugin wouldn't be ideal (if they drift you could have a quick-fix and a command that don't agree).
I don't mind saying no to not having customizable lints, but I do think there might well be value to us adding a lint to enforce using the server's sort feature. If nothing else it would give us a style signal that we could use is several refactors so add new members to the right place in the file so that users didn't have to manually keep their files sorted.
And would also help with pre submits bots that check under DAS for sorted files with a clear IDE sign for maintainers and contributors that some files need sorting.
And would also help with pre submits bots that check under DAS for sorted files with a clear IDE sign for maintainers and contributors that some files need sorting.
When we have a bespoke style (alphabetizing), we can support that with an analyzer plugin.
I wish we didn't offer a 'sort' IDE command, and I wouldn't recommend anyone use it (unless required to), but since we do, we could still offer the lint rule.
I wish we didn't offer a 'sort' IDE command, and I wouldn't recommend anyone use it
I didn't use to like this - the order feels somewhat arbitrary. However in time it's grown on me... sometimes I can't decide even myself how I should order/group things so it avoids me having to think about it.
It also has a slight benefit in that it can reduce conflicts (if multiple people add new fields to "the end of the list of fields" then they may conflict, but if they are sorted, there is more chance they will end up in different places and therefore merge cleanly).
(whether it should be in the SDK versus a plugin, I'm less sure - but even if plugins supported commands, I don't think we could expose them as top-level commands in VS Code, because they have to be declared statically in the extension)
FWIW, I use the automatic IDE sort command all the time (I have a keystroke for it) and not having to think about how to sort things keeps me sane (to a point). As @DanTup says above, it also helps when merging.
For a long time I though the sorting is part of the LSP server. It is not? Does anyone know where the sorting algorithm lives? It may help push someone (me?) to get a community solution going. (If I were to create a sorting tool, I would want it to work, by default, exactly as the current sorting does. And then maybe go from there.)
Activity
bwilkerson commentedon Dec 9, 2020
Thanks for the request!
The linter doesn't support configurable rules. We've talked about adding support before but haven't yet done so.
One of the reasons we don't have a lint to enforce ordering is the lack of consensus as to what the ordering ought to be. Without some way for different teams to define their own preference and a lack of consensus within the community it would be difficult to know what order to support.
Within IntelliJ (I'm not sure about VS Code) there is a "Sort Members in Dart File" command. The team that works on the Dart analyzer and analysis server use that command to enforce an order on the code. It's a bit simpler, grouping declarations by kind and then by name. I mention it because there is at least some level of tooling support for it which might be useful for you.
pq commentedon Dec 9, 2020
Thanks @jibiel for the request and Brian for your response! FWIW, here's the issue tracking configurability, in general, as a feature: #57673. There's some additional context there. Feel free to chime in! (You may also find this long conversation about support for custom rules interesting: #57588.)
As for the request in general, I'd personally very much like a sort-order lint. For my (personal) purposes, one that simply agreed with the order enforced by the "Sort Members in Dart File" command would be a great step. (It's interesting to note that we have an extra step of analysis to enforce this in the analyzer codebase. See: verify_sorted_test. With a lint we could make this go away and it would be easier for others to benefit.)
Aside: I just yesterday introduced some unwanted churn in the linter codebase that would have been avoided if we were enforcing sorting on commit (see: dart-archive/linter#2362).
jibiel commentedon Dec 9, 2020
@bwilkerson Turns out we also have "Dart: Sort Members" within VS Code via Dart extension. But it produces strange results:
importgroups (I like to separate external and internal imports).initStateafterbuild.@pq On it.
incendial commentedon Jan 31, 2021
@jibiel Hi, we are developing additional linting tool for dart https://dcm.dev/ and have configurable member ordering rule https://dcm.dev/docs/rules/common/member-ordering/. It would be great if u could take a look. Any feedback is welcome
NatoBoram commentedon Feb 18, 2021
Similar to
directives_ordering, there should be a linter rule to sort members according to the Dart Style Guide and one according to the Flutter Style Guide.It doesn't need any configuration since that would go against the reasons why linter rules aren't configurable in the first place. Sure, I disagree with lots of the reasoning that both styles have for their chosen ordering, but their goal isn't to satisfy me personally so that's fine. There's no consensus on spaces vs tabs but
dartfmtstill picks one and forces it on everyone and that's fine.The current patchwork of using an external extension (even if it's first-party) to do it isn't viable if it's something that a team wants to be enforced via CI.
I think this issue should be focused specifically on
member_ordering&flutter_member_orderingwithout considering potential configuration :rumax commentedon May 31, 2023
Are there any updates or plans regarding the implementation of sorting? For further inspiration, you can refer to the documentation on sorting components in the eslint-plugin-react repository on GitHub: eslint-plugin-react - sort-comp.md or TypeScript member-ordering
srawlins commentedon May 31, 2023
No updates and no plans that I know of.
pq commentedon May 31, 2023
No updates but this is still something I'd very much like to see happen. As I mentioned in #58283, the absence of this in lint form leads to ad hoc solutions and lots of needless churn. I do wish we could converge on one agreed ordering (and admit that's part of why I haven't pushed harder on this) but could get over that I guess.
@goderbauer: I'm curious, from the flutter perspective how valued this enforcement would be?
goderbauer commentedon May 31, 2023
So far, this hasn't been a huge problem in the Flutter repository. I can't remember the last time I had to ask in a code review to change the order to match Flutter's style. So, while this would be nice to have - and it would be interesting to see where we are currently breaking our own rules - I wouldn't consider it very high priority for us.
rumax commentedon May 31, 2023
I would like to bring to your attention that according to the Flutter Style Guide, it is recommended to have the mentioned rule in place.
If there are no current priorities or plans to incorporate this rule, it would be helpful to officially close the issue with a clear explanation. This will ensure that there are no false expectations or lingering anticipation regarding the implementation of the rule.
pq commentedon May 31, 2023
I'm not in a rush to close this one. I do think it will happen, I just don't know when. It may get easier to implement soon too as we're looking at re-structuring the linter sources to live closer to the analyzer bits they're built on.
That said, I do know it's a drag. I'm with you!
lrhn commentedon Jun 6, 2023
Worth noting that dart-style doesn't have a recommend ordering of members. The only thing it specifies ordering for is imports.
And there is no real consensus either. (
The only ordering I go by is:
The rest is below (and maybe some more statics above).
pq commentedon Jun 6, 2023
Agreed. This makes green-lighting an implementation harder.
Practically speaking, it's worth noting that we do kind of a notion of a canonical ordering as implemented in the "sort members" action.
If I'm not mistaken, the "sort members" ordering is consistent with this.
@scheglov?
8 remaining items
srawlins commentedon Dec 2, 2025
We won't be implementing this as a warning or a lint rule in the Dart analyzer. We now have a high bar for implementing and maintaining built-in static analysis rules, and this rule supports a bespoke style. As of Dart 3.10 (Flutter 3.38), a new analyzer plugins system is available, and the requested static analysis here could be implemented as an analyzer plugin.
DanTup commentedon Dec 3, 2025
@srawlins does that relate to a customisable lint/ordering, or a lint for sorting in general?
I ask because there's a similar request for a fix at #36152 so if the idea is that a plugin should be used to enforce any sorting, probably the same applies there. However, server already has code for sorting (which is accessible as a command to the IDEs), and duplicating that in a plugin wouldn't be ideal (if they drift you could have a quick-fix and a command that don't agree).
srawlins commentedon Dec 3, 2025
A lint for sorting in general.
I will also walk through the quick fix requests, to close out any that should be implemented in an analyzer plugin.
Yeah it would be best if we could re-use that logic. Maybe it could go into the analyzer package, or analysis_server_plugin.
bwilkerson commentedon Dec 3, 2025
I don't mind saying no to not having customizable lints, but I do think there might well be value to us adding a lint to enforce using the server's sort feature. If nothing else it would give us a style signal that we could use is several refactors so add new members to the right place in the file so that users didn't have to manually keep their files sorted.
FMorschel commentedon Dec 3, 2025
And would also help with pre submits bots that check under DAS for sorted files with a clear IDE sign for maintainers and contributors that some files need sorting.
srawlins commentedon Dec 3, 2025
When we have a bespoke style (alphabetizing), we can support that with an analyzer plugin.
I wish we didn't offer a 'sort' IDE command, and I wouldn't recommend anyone use it (unless required to), but since we do, we could still offer the lint rule.
DanTup commentedon Dec 3, 2025
I didn't use to like this - the order feels somewhat arbitrary. However in time it's grown on me... sometimes I can't decide even myself how I should order/group things so it avoids me having to think about it.
It also has a slight benefit in that it can reduce conflicts (if multiple people add new fields to "the end of the list of fields" then they may conflict, but if they are sorted, there is more chance they will end up in different places and therefore merge cleanly).
(whether it should be in the SDK versus a plugin, I'm less sure - but even if plugins supported commands, I don't think we could expose them as top-level commands in VS Code, because they have to be declared statically in the extension)
filiph commentedon Oct 1, 2026
FWIW, I use the automatic IDE sort command all the time (I have a keystroke for it) and not having to think about how to sort things keeps me sane (to a point). As @DanTup says above, it also helps when merging.
For a long time I though the sorting is part of the LSP server. It is not? Does anyone know where the sorting algorithm lives? It may help push someone (me?) to get a community solution going. (If I were to create a sorting tool, I would want it to work, by default, exactly as the current sorting does. And then maybe go from there.)
DanTup commentedon Oct 1, 2026
It is indeed in the analysis server, the
MemberSorterclass is here:https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/lib/src/services/correction/sort_members.dart
filiph commentedon Oct 2, 2026
Ok, so I went ahead and asked an LLM to build a Dart CLI tool out of this. Here it is:
https://pub.dev/packages/sort_dart_members
Obviously, it's LLM throwaway code and I make no promises on maintenance — but it's already being helpful to me in LLM sessions. LLMs can run:
to see if a file is sorted. They can also inspect how to sort a file (from the command's stdout), or they can simply sort files inline.
I made a tiny skill for the LLMs so that they always do this before showing me any code they write.
You can install via
dart pub global activate sort_dart_members. Maybe this helps someone out there.bwilkerson commentedon Oct 2, 2026
@mariamhas