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);
},
);
},
),
);
}