StreamBuilder with Cloud Firestore bad practice?


This StackOverflow answer (How to deal with unwanted widget build?) says that widgets should have no side effects in their build function, and that when using a StreamBuilder, you should not create a stream inline, but initialize in initState in a StatefulWidget.

However, the official cloud_firestore docs show this example: Photo

This example creates the stream in the build function.

Is this truly bad practice, and the Firestore team is just wrong here, or is Firestore doing something under the hood that I don’t know about that makes this okay?


This practice is perfectly normal. The post that you linked to doesn’t say not to create Streams inline. That is only applicable for Futures and is made clear by the Flutter Docs in the FutureBuidler class.

Answered By – Christopher Moore

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.