SylvainJack
SylvainJack

Reputation: 1257

Flutter / Firestore - How can I reach nested elements on Firebase?

I am working on an English vocabulary learning app.
I created some data classes to create different objects : Here are two of them :

class CarnetWords {
  int? iD;
  int? wrong;
  int? right;
  double? mastery;
  CarnetWords(
      {@required this.iD,
      @required this.wrong,
      @required this.right,
      @required this.mastery});
}

class VocList {
  String? ref;
  String? titre;
  DateTime? creation;
  DateTime? modification;
  DateTime? firstSession;
  DateTime? latestSession;
  double? mastery;
  List<CarnetWords>? wordId;
  VocList(
      {@required this.ref,
      @required this.titre,
      @required this.creation,
      @required this.modification,
      @required this.firstSession,
      @required this.latestSession,
      @required this.mastery,
      @required this.wordId});
}

My problem is that I need to upload these elements to firebase. It works fine : I manage to "transform" my objects into maps etc... Here is what it looks like on firebase :

Here is what it looks like on FIREBASE (See picture

Now I need to retrieve these elements and I just can't figure out how to reach inside the "mots" list and create my list of objects. Here is my code so far. What I tried with the ['id']... is obviously not working...

final _fireStore3 = FirebaseFirestore.instance
        .collection('familyAccounts')
        .doc(id)
        .collection('users')
        .doc('user2')
        .collection('vocList');
    await _fireStore3.get().then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        _carnetVoc2.add(
          VocList(
            ref: doc['ref'],
            titre: doc['titre'],
            creation: doc['dateCreation'].toDate(),
            modification: doc['dateModification'].toDate(),
            firstSession: doc['firstSession'].toDate(),
            latestSession: doc['latestSession'].toDate(),
            mastery: doc['mastery'].toDouble(),
            wordId: [
              CarnetWords(
                  iD: doc['mots']['id'],
                  wrong: doc['mots']['wrong'],
                  right: doc['mots']['right'],
                  mastery: doc['mots']['mastery'].toDouble())
            ],
          ),
        );
      });
    });

Upvotes: 0

Views: 618

Answers (2)

SylvainJack
SylvainJack

Reputation: 1257

I managed to solve the problem like this :

await _fireStore3.get().then((QuerySnapshot querySnapshot) {
      querySnapshot.docs.forEach((doc) {
        List<dynamic> mots = doc['mots'].toList();
        List<CarnetWords> wordId = [];
        for (Map indivMots in mots) {
          wordId.add(
            CarnetWords(
                iD: indivMots['id'],
                wrong: indivMots['wrong'],
                right: indivMots['right'],
                mastery: indivMots['mastery']),
          );
        }

Upvotes: 1

Jorge Vieira
Jorge Vieira

Reputation: 3064

Maybe works this way, first you iterate over the itens of the list and then add the list to the object,

await _fireStore3.get().then((QuerySnapshot querySnapshot) {
  querySnapshot.docs.forEach((doc) {
    List<CarnetWords> carnetWords = [];
    Map mots = doc['mots'];
    
    mots.forEach((index, value){
      carnetWords.add(CarnetWords(id: value.id, wrong: value.wrong, right: value.wrong, mastery: value.mastery.toDouble()));
    });
    
    _carnetVoc2.add(
      VocList(
        ref: doc['ref'],
        titre: doc['titre'],
        creation: doc['dateCreation'].toDate(),
        modification: doc['dateModification'].toDate(),
        firstSession: doc['firstSession'].toDate(),
        latestSession: doc['latestSession'].toDate(),
        mastery: doc['mastery'].toDouble(),
        wordId: carnetWords,
      ),
    );
  });

}

Upvotes: 1

Related Questions