Home > Mobile >  print values from array field from firestore
print values from array field from firestore

Time:05-31

how can I display all of the data from the courses array to the user

this is my function

printallvaluesfromarray() {

    var courses = FirebaseFirestore.instance
        .collection('CurrentCourses')
        .doc(user.uid)
        .get()
        .then((value) {
      return value['courses'];
    });
  }

CodePudding user response:

value['courses'].forEach((val) {
print(val);
});

// Can you try this

CodePudding user response:

You can try this,

printallvaluesfromarray() {

    var courses = FirebaseFirestore.instance
        .collection('CurrentCourses')
        .doc(user.uid)
        .get()
        .then((value) {
        print(value.toString())
      return value['courses'];
    });
  }

CodePudding user response:

You are correctly fetching the document of the user, all you need is to just print the value like the following:

Future<void> printAllValuesFromArray() {
  return FirebaseFirestore.instance
      .collection('CurrentCourses')
      .doc(user.uid)
      .get()
      .then((document) {
    final courses = document['Courses'];
    for (var course in courses) {
      print(course);
    }
  });
}

Note: You were using incorrect key to access the courses from the document which was courses while the correct one is Courses with capital C,

  • Related