Skip to content

Enforce consistent order of class members #58283

Open

Description

@jibiel

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 rule is not specific to any Dart framework.

Activity

  1. bwilkerson commented on Dec 9, 2020

    @bwilkerson
    Member

    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.

  2. pq commented on Dec 9, 2020

    @pq
    Member

    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).

  3. jibiel commented on Dec 9, 2020

    @jibiel
    Author

    @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).
    • Placing initState after build.

    @pq On it. :neckbeard:

  4. incendial commented on Jan 31, 2021

    @incendial
    Contributor

    @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

  5. NatoBoram commented on Feb 18, 2021

    @NatoBoram

    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
  6. rumax commented on May 31, 2023

    @rumax

    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

  7. srawlins commented on May 31, 2023

    @srawlins
    Member

    No updates and no plans that I know of.

  8. pq commented on May 31, 2023

    @pq
    Member

    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?

  9. goderbauer commented on May 31, 2023

    @goderbauer
    Contributor

    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.

  10. rumax commented on May 31, 2023

    @rumax

    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.

  11. pq commented on May 31, 2023

    @pq
    Member

    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!

  12. lrhn commented on Jun 6, 2023

    @lrhn
    Member

    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:

    1. Public static final/const variables. Possibly constants before non-constants, but it's rare to have public non-constants.
    2. Instance variables. Semantic ordering (related variables grouped).
    3. Constructors. Public before private.

    The rest is below (and maybe some more statics above).

  13. pq commented on Jun 6, 2023

    @pq
    Member

    Worth noting that dart-style doesn't have a recommend ordering of members.

    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.

    The only ordering I go by is:

    If I'm not mistaken, the "sort members" ordering is consistent with this.

    @scheglov?

  14. 8 remaining items

  15. srawlins commented on Dec 2, 2025

    @srawlins
    Member

    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.

  16. DanTup commented on Dec 3, 2025

    @DanTup
    Collaborator

    @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).

  17. srawlins commented on Dec 3, 2025

    @srawlins
    Member

    does that relate to a customisable lint/ordering, or a lint for sorting in general?

    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.

    However, server already has code for sorting ..., and duplicating that in a plugin wouldn't be ideal

    Yeah it would be best if we could re-use that logic. Maybe it could go into the analyzer package, or analysis_server_plugin.

  18. bwilkerson commented on Dec 3, 2025

    @bwilkerson
    Member

    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.

  19. FMorschel commented on Dec 3, 2025

    @FMorschel
    Contributor

    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.

  20. srawlins commented on Dec 3, 2025

    @srawlins
    Member

    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.

  21. DanTup commented on Dec 3, 2025

    @DanTup
    Collaborator

    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)

  22. filiph commented on Oct 1, 2026

    @filiph
    Contributor

    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.)

  23. DanTup commented on Oct 1, 2026

    @DanTup
    Collaborator

    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 is indeed in the analysis server, the MemberSorter class is here:

    https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/lib/src/services/correction/sort_members.dart

  24. filiph commented on Oct 2, 2026

    @filiph
    Contributor

    https://github.com/dart-lang/sdk/blob/main/pkg/analysis_server/lib/src/services/correction/sort_members.dart

    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:

    sortdart --check path/to/file.dart
    

    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.

    sortdart -w path/to/file.dart
    

    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.

  25. bwilkerson commented on Oct 2, 2026

    @bwilkerson
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposaltype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions