简体   繁体   中英

How to access a particular field of other document which is in some "abc" Collection from some other "xyz" Collection in Firestore Flutter?

I'm stuck in between of a flutter project, can someone please help me in solving this issue?For this project I'm using firestore as a database.

Quick Summary of the project: one app - > Two different account types.(say it as student and tutor )

tutor - generates a QR code. ( whenever QR code is generated, a field named as "TotalClassesTook" is created and maintains a value which is the number of QR codes generated by that particular tutor)

student - scans the QR code.( whenever QR code is scanned, a field named as "TotalClassesAttended" is created and maintains a value which is the number of QR codes scanned)

My Requirement Now: Now in the student account type(which is in "Students" collection), how can i get the"TotalClassesTook" field values(which is in different collection named as "Tutors") and calculate attendance percentage for that particular student and display it in the student account?

Please let me know the way if that's possible

FireStore Structure of project

学生

导师

Like I mentioned in my comment, I think your logic is flawed, there is no link between tutor and student in the structure you shared, other than tutorid in the student collection which is null, so there is no way to know which tutor document to look for per each student.

Although you should come up with a solution to this yourself as only you will know what are the requirements of your app, I can give you some insights in how I would do this:

First you need to create a link between a student and all the tutors, assuming that a tutor can only teach one class (and if this not true you can create a second identifier like classname of something like that) you could create a subcollection of student where each subdocument will act like a student information for that specific class, so you will have this structure:

students Collection
    email
    fullName
    mobileNumber
    role
    rollNumber
    studentClass Subcolletion
        tutorId
        totalClassesAttended
        present

At this point you can get the StudentClass document's totalClassesAttended to calculate the attendence like this:

FirebaseFirestore.instance
                 .collection('students')
                 .doc('YOUR_STUDENT_ID')
                 .collection('studentClass')
                 .where('tutorId', isEqualTo: 'YOUR_TUTOR_ID')
                 .get()
                 .then((snapshot) {
                     //this will be the value you want
                     String classesAttended = snapshot.data['totalClassesAttended'].toString();
                 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM