New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
packages/flutter/lib/src/widgets/async.dart video promotes bad use of FutureBuilder #112676
Comments
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. |
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 |
CC @craiglabenz |
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 |
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. :) |
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. |
I'm a bit confused here. @RandalSchwartz is this the code you are referring to? At least from what I can tell here, this |
The problem is that FutureBuilder lets the future: parameter change at will, and will start watching the new Future instead. This is by design, and makes sense. But, the example from the video will create a different Future every time the enclosing widget's build() is run, so every new Future replaces the Future that FutureBuilder was watching. And, the docs summarize this accurately as:
|
Right, yes, I see your point @RandalSchwartz . The call will spawn potentially many |
And that's what makes the misunderstanding scary. People can get code that works because of little or no rebuilding. And then they add an animation, and get re-futured 60 times per second! |
Yes very good point |
I like the new video! https://youtu.be/zEdw_1B7JHY |
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! |
Perhaps you may be interested in this PR/Issue. This issue partially inspired this new widget. Comments and reactions welcome. |
Fixes flutter#112676. Fixes flutter#97015. Fixes flutter#62107. Fixes flutter#38740. Fixes flutter#31911. Fixes flutter#29958. Fixes flutter#18108. Fixes flutter#17160. Fixes flutter#14243. Fixes flutter#3354.
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 |
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 timebuild(...)
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.
The text was updated successfully, but these errors were encountered: