0

I would like to save a single document field into a local variable, but I am not able to do that. Here is my code:

void getPostsData() async{
    List<Widget> listItems = [];
    String _title;
    String _content;
    final QuerySnapshot result = await Firestore.instance.collection('Social_Posts').getDocuments();
    final List<DocumentSnapshot> documents = result.documents;
    documents.forEach((data){
      listItems.add(
          GestureDetector(
            onTap: () async{
              print(data["postTitle"]);
              print(data["postContent"]);
              setState(() {
               data["postTitle"] == _title;
               data["postContent"] == _content;
              });
              print(_title);
            },
       )
    );
      }

Whenever I try to print out "_title" or "_content", I get null. Why is that happening and how do I fix this?

4
  • As far as I can tell, your code never assigns a value to either _title or _content. It's unclear what to do to fix this. Please edit your question to describe your intent. What is the goal with this code? Commented Jul 11, 2020 at 21:50
  • whats the output of print(documents.length) ? Commented Jul 11, 2020 at 21:54
  • I have attempted to save the field value to the 2 variables in the setState function. That is not working. I would like to save the value of data["postTitle"] to _title and data["postContent"] to content. @Doug Stevenson Commented Jul 11, 2020 at 21:54
  • the output of document.length is 15 @P4yam Commented Jul 11, 2020 at 21:56

1 Answer 1

1

You should probably change

       data["postTitle"] == _title;
       data["postContent"] == _content;

to:

       _title = data["postTitle"];
      _content= data["postContent"];
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much this worked. Just out of curiosity, what is the difference between "data["postTitle"] = _title;" and " _title = data["postTitle"];" Please let me know. Thanks!
@AneeshYalgi == is comparison, if you wanna compare x and 2, you do if (x == 2) and the output of this would be a true or false

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.