Flutter&Firestore-更新永不停止(从一个值切换到另一个)

时间:2018-12-17 16:11:04

标签: firebase dart flutter google-cloud-firestore

我是Dart和Flutter的新手,我正在尝试使用Flutter和Firebase(因此将Firestore作为数据库)构建一个简单的应用程序。

这就是我要实现的目标:


  • 我希望学生能够选择学校
  • 选择后,我将学生的学校更新为学校的documentID参考(某些由Firestore自动生成的UID)。例如:学校:“ eGGDjg2JKFWiLfgvHpeE”。我还给学生附上了电子邮件和电话号码。
  • 就是这样。

问题是:学校的价值不断在Firestore上更新本身。我目前有2所学校,并且在这2个值之间切换(+它使用了大量的Firebase免费套餐编写操作)。

最后但并非最不重要的一点是,该算法位于“选择学校”页面上,但是如果我重新加载该应用程序,则当我转到此页面时(无需点击ListTile,它将自动触发changeSchool函数(请参见下文) !),但不是我在卸载-重新安装后第一次启动该应用程序。(让我知道是否不清楚,我已经呆了几个小时了。)

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

这是触发此功能的原因:

  child: ListTile(
      title: Text(school.name),
      trailing: Text(school.studentsNumber),
      onTap: () async {
        await changeSchool(school);
        Navigator.pop(context);
      },
    ),

我是新手,如果有更好的方法可以做到这一点,我也很开放!

谢谢!

1 个答案:

答案 0 :(得分:0)

因此,我能够使用getDocuments()而不是snapshots()。listen()对其进行修复。这是其他人遇到的问题的代码

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