Skip to content

packages/flutter/lib/src/widgets/async.dart video promotes bad use of FutureBuilder #112676

Closed

Description

@RandalSchwartz

The "Widget Of The Week" video for FutureBuilder (https://youtu.be/ek8ZPdWj4Qo) suggests a usage of FutureBuilder where the future will be created by a function call. This is in direct contradiction of the first two paragraphs of the documentation, as well as common sense. The future cannot be built as the future: parameter to FutureBuilder, because it would be re-built every time build(...) of the enclosing widget is run.

As a result, I think there's a massive misinformation meme running around now. I have a short video on why this is broken, and how to fix it, and it's now getting 10-20 references per day counting both Stack Overflow and the Flutter Discord (https://youtu.be/sqE-J8YJnpg).

My recommendation: immediately pull the video from the docs. Ideally, replace it with a proper video that shows the future coming from a variable. But saying nothing is better than saying the wrong thing, and the first two paragraphs of the documentation explain it properly anyway.

Activity

  1. jonahwilliams commented on Sep 30, 2022

    @jonahwilliams
    Contributor

    Yeah that video is pretty wrong, we should do better by either showing a Future that is cached in a StatefulWidget, or (even better) one that is cached in some sort of service layer.

  2. RandalSchwartz commented on Sep 30, 2022

    @RandalSchwartz
    Author

    Yeah that video is pretty wrong, we should do better by either showing a Future that is cached in a StatefulWidget, or (even better) one that is cached in some sort of service layer.

    @slightfoot and I have been discussing adding a 'create:' to both FutureBuilder and StreamBuilder which would take a callback function to create the Future or Stream, which would get called only if no value is provided, and held in the builder. This would solve 95% of the cases we've seen done wrong, and it would be backwards compatible.

  3. jonahwilliams commented on Sep 30, 2022

    @jonahwilliams
    Contributor

    In order to cache the future/stream in the State object? That would still have issues if there is any stateloss, but that is still probably better than the situation we're in today. Happy to review a PR

  4. added
    in triagePresently being triaged by the triage team
    frameworkflutter/packages/flutter repository. See also f: labels.
    d: api docsIssues with https://api.flutter.dev/
    and removed
    in triagePresently being triaged by the triage team
    on Sep 30, 2022
  5. HansMuller commented on Sep 30, 2022

    @HansMuller
    Contributor
  6. craiglabenz commented on Sep 30, 2022

    @craiglabenz
    Contributor

    Thanks for raising this, @RandalSchwartz. But for your attention to detail, we may not have ever really noticed this; and because of your attention to detail, a refreshed episode for FutureBuilder is coming very soon.

  7. added
    P2Important issues not at the top of the work list
    on Oct 4, 2022
  8. satvikpendem commented on Oct 5, 2022

    @satvikpendem

    I'd also add in the new video to show what could happen were it the case that the future or stream is referenced directly in the build method, like @RandalSchwartz's video shows with incrementing the counter and the future or stream running again. The video would then show the correct solution and show that the future or stream does not run again when placed in a variable.

  9. RandalSchwartz commented on Oct 5, 2022

    @RandalSchwartz
    Author

    I'd also add in the new video to show what could happen were it the case that the future or stream is referenced directly in the build method, like @RandalSchwartz's video shows with incrementing the counter and the future or stream running again. The video would then show the correct solution and show that the future or stream does not run again when placed in a variable.

    I would disagree. The "Flutter of the week" series works well because it merely alerts you to the presence and relative scope of a particular widget, and does not purport to be a "how to use" tutorial except in passing. The subtleties to explain the relationship of build with FutureBuilder and StreamBuilder cannot be adequately represented in this context.

    I'd be happy with the video just not showing a bad practice. :)

  10. RandalSchwartz commented on Oct 29, 2022

    @RandalSchwartz
    Author

    So, the problem hasn't gone away. I'm still seeing people copying bad behavior daily. @slightfoot and I have chatted a bit about adding a "create:" callback to FutureBuilder/StreamBuilder, but we've both had busy schedules.

  11. MelbourneDeveloper commented on Nov 4, 2022

    @MelbourneDeveloper

    I'm a bit confused here. FutureBuilder is a StatefulWidget. That means that all things being equal, it will hold onto the state across rebuilds.

    image

    image

    @RandalSchwartz is this the code you are referring to?

    image

    At least from what I can tell here, this FutureBuilder will get the result of the HTTP call and then hold onto that state across rebuilds. Is there something I'm missing here?

  12. 3 remaining items

  13. MelbourneDeveloper commented on Nov 5, 2022

    @MelbourneDeveloper

    Yes very good point

  14. RandalSchwartz commented on Nov 17, 2022

    @RandalSchwartz
    Author

    I like the new video! https://youtu.be/zEdw_1B7JHY
    I presume it will be incorporated in docstring for FutureBuilder, which will satisfy the initial concern for my initiating this issue. We still should discuss adding a create callback for FutureBuilder and StreamBuilder to avoid the need for visible state variables to hold the future or stream. Should that be from another ticket?

  15. self-assigned this
    on Mar 16, 2023
  16. Hixie commented on Mar 16, 2023

    @Hixie
    Contributor

    The API docs now refer to the new video, and I'll remove the link to the old one.

    For API changes, please consider making a new issue, yes. Thanks!

  17. MelbourneDeveloper commented on Mar 16, 2023

    @MelbourneDeveloper

    @Hixie @RandalSchwartz

    Perhaps you may be interested in this PR/Issue. This issue partially inspired this new widget. Comments and reactions welcome.

    #121962

    #121963

  18. github-actions commented on Apr 6, 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 Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listd: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.r: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions