Flutter & Firestore - Update never stops (switch from one value to another)
So, I was able to fix it using getDocuments() instead of snapshots().listen(). Here's the code if anyone else has this issue
Future<void> changeSchool(school) async {
CollectionReference schoolCollection =
Firestore.instance.collection('School');
CollectionReference studentCollection =
Firestore.instance.collection('Student');
QuerySnapshot schoolQuery = await schoolCollection
.where('name', isEqualTo: school.name)
.where('city', isEqualTo: school.city)
.where('country', isEqualTo: school.country)
.getDocuments();
QuerySnapshot studentQuery = await studentCollection
.where('email', isEqualTo: globals.currentUser.email)
.getDocuments();
final DocumentReference studentRef =
studentCollection.document(studentQuery.documents[0].documentID);
final DocumentReference ref =
schoolCollection.document(schoolQuery.documents[0].documentID);
globals.currentSchool = School.fromSnapshot(await ref.get());
Student tmpStudent = Student.fromSnapshot(await studentRef.get());
tmpStudent.school = ref.documentID;
Firestore.instance.runTransaction((transaction) async {
await transaction.update(studentRef, tmpStudent.toJson());
});
}
Lorris
Updated on December 08, 2022Comments
-
Lorris over 1 year
I'm new to Dart and Flutter and I'm trying to build a simple App using Flutter and Firebase (thus, Firestore as database).
Here's what I'm trying to achieve :
- I want a student to be able to pick a school
- When picked, I update the student's school to be the documentID reference of the school (some auto-generated UID by Firestore). example : school: "eGGDjg2JKFWiLfgvHpeE" . I also have an email and phone number attached to student.
- That's it.
The thing is : the school value keeps on updating itself on Firestore. I have 2 schools for now, and it switches between those 2 values (+ it uses a lot of Firebase free tier writing actions).
Last but not least, this algorithm is on a "Pick your school" page, but if I reload the App, it will auto-trigger the changeSchool function (see below) when I go to this page (WITHOUT tapping the ListTile !), but not the first time I start the app after uninstalling-reinstalling.. (Let me know if it's unclear, I've been there for hours).
Future<void> changeSchool(school) async { CollectionReference schoolCollection = Firestore.instance.collection('School'); CollectionReference studentCollection = Firestore.instance.collection('Student'); schoolCollection .where('name', isEqualTo: school.name) .where('city', isEqualTo: school.city) .where('country', isEqualTo: school.country) .snapshots() .listen((data) { if (data.documents.length == 1) { studentCollection .where('email', isEqualTo: globals.currentUser.email) .snapshots() .listen((students) async { final DocumentReference studentRef = studentCollection.document(students.documents[0].documentID); final DocumentReference ref = schoolCollection.document(data.documents[0].documentID); globals.currentSchool = School.fromSnapshot(await ref.get()); Student tmpStudent = Student.fromSnapshot(await studentRef.get()); tmpStudent.school = ref.documentID; Firestore.instance.runTransaction((transaction) async { await transaction.update(studentRef, tmpStudent.toJson()); }); }); } });
Here's what supposed to trigger this function :
child: ListTile( title: Text(school.name), trailing: Text(school.studentsNumber), onTap: () async { await changeSchool(school); Navigator.pop(context); }, ),
As I'm new, if there's a better way of doing this, I'm also very open !
Thanks !