skip to Main Content

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


  1. 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 :

    FirebaseFirestore.instance.doc(documentPath).get(); // Future
    
    FutureBuilder<DocumentSnapshot>(
      future: FirebaseFirestore.instance.doc(documentPath).get(),
      builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.hasData) {
          return Text("${snapshot.data!.data()}");
        }
        return Text("something else");
      },
    );
    

    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 for DocumentSnapshot and CollectionSnapshot, 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:

    FirebaseFirestore.instance.doc(documentPath).snapshots(), // Stream
    
    
    StreamBuilder<DocumentSnapshot>(
      stream: FirebaseFirestore.instance.doc(documentPath).snapshots(), // Stream
      builder: (context, AsyncSnapshot<DocumentSnapshot> snapshot) {
        if (snapshot.connectionState == ConnectionState.active) {
        final currentData = snapshot.data!.data() as Map<String, dynamic>;
         final fieldValue = currentData["fieldString"];
          return Text("$fieldValue");
        }
        return Text("something else");
      },
    );
    

    change fieldString with your field name to get its value.

    Now every update you do to the DocumentSnapshot, the StreamBuilder will get that new snapshot from the Stream and update the UI based on it.

    Hope this helps!

    Login or Signup to reply.
  2. you could also listen to those snapshots without using StreamBuilder or any Flutter widget with the listen() method like this:

    final fieldValue = "initial value";
        FirebaseFirestore.instance
            .doc(documentPath)
            .snapshots()
            .listen((snapshot) {
          final data = snapshot.data() as Map<String, dynamic>;
          fieldValue = data["field"];
          print(fieldValue);
        });
    

    the fieldValue will be updated now every time an update happens to the document, and it will print it automatically in the console with the print()

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search