Lorris
Lorris

Reputation: 73

Flutter & Firestore - Update never stops (switch from one value to another)

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 :



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 !

Upvotes: 2

Views: 2008

Answers (1)

Lorris
Lorris

Reputation: 73

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

Upvotes: 1

Related Questions