1
votes

Firestore document contains particular user information (username, age, challenges, etc). Of course, one user can have multiple challenges. I am loading particular user data into a list of objects but the problem occurs when I return that list to a FutureBuilder. Let's say I have 2 users and each has 3 challenges. When I return the list it shows the right length (6) but contains only the data from the last user that was loaded into a list. My ListView.builder displays 6 _cardBuilders but the data in those cards is the same.

I've tried to replace the list of constructors with a list of Map but the outcome is the same. It's probably a logical mistake but I'm unable to find it.

Future<List<AllChallengesStorage>> challengesToFeed () async {

    var queryResult = await Firestore.instance.collection("Users")
        .where("total_challenges", isGreaterThanOrEqualTo: 1)
        .getDocuments();

    if (queryResult.documents.isNotEmpty){  

      for (int i=0; i < queryResult.documents.length; ++i){
        for (int j=0; j < queryResult.documents[i].data['total_challenges']; ++j){

          widget.challengesInfo.username = queryResult.documents[i].data['username'];
          widget.challengesInfo.name = queryResult.documents[i].data['challenges'][j]['name'];
          widget.challengesInfo.description = queryResult.documents[i].data['challenges'][j]['description'];
          widget.challengesInfo.duration = queryResult.documents[i].data['challenges'][j]['duration'];

          allChallenges.add(widget.challengesInfo);
        }
      }
    }
    return allChallenges;
  }


class _FeedState extends State<Feed> {

  List<AllChallengesStorage> allChallenges = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder<List<AllChallengesStorage>>(
        future: challengesToFeed(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (!snapshot.hasData) return Center(child: Text("Loading..."));
          return ListView.builder(
            itemCount: snapshot.data.length,
            itemBuilder: (BuildContext context, int index) {
              return _cardBuilder(context, snapshot, index);
            },
          );
        },
      ),
    );
  }
1

1 Answers

0
votes

The problem is that you are using the same object and just overwriting it's properties on each loop. What you need to is instantiate a new object for every new card like Create a new helper object with your model class every time your second loops is completed.

    for (int i=0; i < queryResult.documents.length; ++i){
       ClassName helper = ClassName();
    for (int j=0; j < queryResult.documents[i].data['total_challenges']; ++j){

      helper.username = queryResult.documents[i].data['username'];
      helper.name = queryResult.documents[i].data['challenges'][j]['name'];
      helper.description = queryResult.documents[i].data['challenges'][j]['description'];
      helper.duration = queryResult.documents[i].data['challenges'][j]['duration'];

      allChallenges.add(helper);
    }
  }
}

Then to access the objects in the list, you can specify the index and access the object at that index and it's properties

allChallenges[index].name