After defining a variable ("xyz" at screenshot), I fetched some data on my firestore database, then changed that variable value to data i fetched from firestore. When I print the changed variable with "print()" it appears at "Run" the value I fetched, which is what I want. But when I run the app, the text I assigned as changed variable appears on the screen with old value like I never changed it after defining. the code
When I print(xyz); it appears as the data from firestore, so there is no problem at database connection. I just want to update the value appears at screen too.
2
Answers
as I see in the screenshot, you’re getting your document’s data with a
Future
, and the Future is useful when you need to call data just once :This will get the snapshot only once, even if you updated the document data, nothing will update on the screen until you make manually a new request to get another snapshot.
well the Firebase SDK for Flutter provide also
Stream
of snapshots forDocumentSnapshot
andCollectionSnapshot
, which listen directly to it, so whenever you have an update on your target, that stream will be notified and provide a new snapshot for that new updated data, and you can use it like this:change
fieldString
with your field name to get its value.Now every update you do to the
DocumentSnapshot
, theStreamBuilder
will get that new snapshot from theStream
and update the UI based on it.Hope this helps!
you could also listen to those snapshots without using
StreamBuilder
or any Flutter widget with thelisten()
method like this:the
fieldValue
will be updated now every time an update happens to the document, and it will print it automatically in the console with theprint()