0
votes

I have a list of a document ids and I want to fetch the data of those documents from Firestore and display it using the FutureBuilder.

contestList = [awebnmsdfjkeeer23,324cdas4asdf, 34sdfasgadsg]
Future<void> fetchUsergameData() async {

    contestList.forEach((element) async{ 
        await Firestore.instance.collection('LiveGames').document('$element')
            .get().then((dss) {
                if(dss.exists) {
                    tempgame.add(dss.data["GameData"]);
                    temproom.add(dss.data["Room"]);
                    temptitle.add(dss.data["Title"]);
                    temp = tempgame + temproom + temptitle;
                    joinedContests.add(temp);
                }
            }).then((value) => {});
        });

        print(joinedContests);

    }
}

I have used the above function to get the data and try to store in the list, like one document data in list. But i am getting the blank list of the data. How to get the whole document and display it using the FutureBuilder in flutter

1
I am quite confused with the first line. Are those supposed to be strings or variables? Either way, you won't need '$element' just element. Maybe that's why you are not getting the document (wrong/uninteded id).VLXU

1 Answers

0
votes

It looks like you have multiple different issues on your code:

  • contestList has invalid keywords. 324cdas4asdf and 34sdfasgadsg are not valid variable names as they both start with a number, which is not a valid variable name. If they are supposed to be the ids that you want to retrieve they must be enclosed by ", which will make them strings.
  • You are trying to access the document using '$element' as if it were a bash variable, but there are two problems there: it's not done like that and there no need to do it. element already holds the value as a string so it just has to be accessed as is.
  • You are calling the method then twice without doing anything the second time. This shouldn't be a concern, but it simply doesn't do anything and can me omitted.

Below you will see an edited version of your code fixing all the aforementioned mistakes.

contestList = ["awebnmsdfjkeeer23", "324cdas4asdf", "34sdfasgadsg"]
Future<void> fetchUsergameData() async {

    contestList.forEach((element) async{ 
        await Firestore.instance.collection('LiveGames').document(element)
            .get().then((dss) {
                if(dss.exists) {
                    tempgame.add(dss.data["GameData"]);
                    temproom.add(dss.data["Room"]);
                    temptitle.add(dss.data["Title"]);
                    temp = tempgame + temproom + temptitle;
                    joinedContests.add(temp);
                }
            });
        });

        print(joinedContests);

    }
}

On another note, it's unknown to us the type of tempgame, temproom and temptitle but judging by how you are accessing it you may simply want to do something like this:

tempgame = dss.data["GameData"];
temproom = dss.data["Room"];
temptitle = dss.data["Title"];
temp = tempgame + temproom + temptitle;
joinedContests.add(temp);