0

I have an int named "length" in my code and I am trying to change it's value from a field in my Cloud Firestore:

 int length;
_handlePressed(context) {
    DocumentReference postReference =  Firestore.instance.collection(ISBN).document(post);

    postReference.get().then((datasnapshot){
      if(datasnapshot.exists) {
        length = datasnapshot.data["length"];
        print(length.toString());
      }
    });
}

The field "length" is stored as a Number Type in my Firestore.

The problem is that the print operation does not execute and printing length elsewhere shows null in the console. What am i missing?

1 Answer 1

1

First, make sure that _handlePressed is really called then update the state with setState when using a StatefulWidget:

int length; 
_handlePressed(context) { 
    DocumentReference postReference = Firestore.instance.collection(ISBN).document(post); 
    postReference.get().then((datasnapshot){ 
    if(datasnapshot.exists) { 
        setState(()
            length = datasnapshot.data["length"];
        );
    print(length.toString()); 
} }); }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.