The FutureBuilder doc says
The future must have been obtained earlier, e.g. during State.initState, State.didUpdateWidget, or State.didChangeDependencies. It must not be created during the State.build or StatelessWidget.build method call when constructing the FutureBuilder. If the future is created at the same time as the FutureBuilder, then every time the FutureBuilder's parent is rebuilt, the asynchronous task will be restarted.
I'm guessing the same is true for StreamBuilder but that doc doesn't give similar guidance. Is StreamBuilder fundamentally different from FutureBuilder in this regard?
Thanks!
Yes, you should also initiate the
StreamBuilderwithin theinitState, as discussed in my previous Q/A aboutFutureBuilder.This approach prevents unnecessary repainting of your UI and
StreamBuilderwith each rebuild (for example, when triggered bysetState).The reason (I think) why it's only updated in the documentation for
FutureBuilderis since they needed to edit the documentation after a proposed issue explaining why you should useinitState.