Skip to content

Add a "design discussion" section to the setState method #12296

Description

@konmik

I just started playing with Flutter, and the most obvious stuff is setState. Honestly, it does not look good. Why would you want to put all your mutations into a lambda, especially if they are all side-effecting? Wouldn't it be better just to call update() directly after you do all the state mutations?

The pattern looks like it came from a functional architecture, but the main point is missing - state mutation should have been encapsulated inside setState function, but this is not the case for Flutter. Without immutability, pure lambda and state change encapsulation all of this doesn't seems have any meaning.

Activity

  1. Hixie commented on Sep 29, 2017

    @Hixie
    Contributor

    We used to just have a markNeedsBuild method but we found people called it like a good luck charm -- any time they weren't sure if they needed to call it, they'd call it.

    We changed to a method that takes a (synchronously-invoked) callback, and suddenly people had much less trouble with it. We found people understand much more easily that if you didn't change any state, you don't need to call it; if you did change some state, you call it and change the state during the call.

    Eventually we plan to make some lints verify that anything you use in your build function you only set from setState, to catch cases where people set things but don't notify the framework.

  2. added this to the 3: Current Milestone milestone on Sep 29, 2017
  3. Hixie commented on Sep 29, 2017

    @Hixie
    Contributor

    I'll add a "Design discussion" section to the setState docs talking about this. Let me know if there's anything else you'd like covered in the docs for this, I'm happy to add more information there.

  4. konmik commented on Sep 29, 2017

    @konmik
    Author

    Maybe the better docs was needed for markNeedsBuild? Or just rename it to onStateChanged() or whatever? Lambda instantiation is not free, it adds needless code nesting, it also increases cognitive code complexity and puzzles more experienced devs (me :D).

    Otherwise, thanks for the explanation! :)

  5. Hixie commented on Sep 29, 2017

    @Hixie
    Contributor

    Maybe the better docs was needed for markNeedsBuild?

    Maybe, but in my experience we shouldn't depend on people reading API docs. :-)

    Or just rename it to onStateChanged() or whatever?

    That would be inconsistent with our naming guide: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#naming-rules-for-typedefs-and-function-variables

    Lambda instantiation is not free

    It's a pretty small cost when called once per frame (which is what you generally expect for setState). Also, it's not called during performance-critical periods; it's called in response to user input generally and so we have more CPU to play with here than if it was during the frame itself. Also eventually I wouldn't be surprised if we were able to get the compiler to just inline this.

    it adds needless code nesting

    Needless is subjective here. I would argue the need is to show that this is the block that is actually mutating the state.

    it also increases cognitive code complexity

    Well as noted above, in practice it seems to have actually reduced it.

    and puzzles more experienced devs (me :D).

    Yup, that's why we need to add more to the documentation. :-)

  6. changed the title [-]Why setState?[/-] [+]Add a "design discussion" section to the setState method[/+] on Dec 20, 2017
  7. modified the milestones: 3: Current Milestone, on Dec 20, 2017
  8. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Dec 20, 2017
  9. tvolkert commented on Jan 10, 2020

    @tvolkert
    Contributor

    Closing, as there are several places in our documentation nowadays that talk about setState() and state management.

  10. mateusfccp commented on Apr 19, 2021

    @mateusfccp
    Contributor

    Eventually we plan to make some lints verify that anything you use in your build function you only set from setState, to catch cases where people set things but don't notify the framework.

    @Hixie Is there any issue tracking this?

  11. github-actions commented on Aug 2, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  12. locked as resolved and limited conversation to collaborators on Aug 2, 2021
  13. 9 remaining items

  14. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Jul 30, 2023
  15. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Aug 7, 2023
  16. added a commit that references this issue on Aug 10, 2023
  17. added a commit that references this issue on Aug 10, 2023
  18. github-actions commented on Aug 24, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  19. locked as resolved and limited conversation to collaborators on Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P1High-priority issues at the top of the work listd: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.team-frameworkOwned by Framework teamwaiting for PR to land (fixed)A fix is in flight

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions