Firebase 颤振与振动;Firestore-更新永不停止(从一个值切换到另一个值)

Firebase 颤振与振动;Firestore-更新永不停止(从一个值切换到另一个值),firebase,dart,flutter,google-cloud-firestore,Firebase,Dart,Flutter,Google Cloud Firestore,我不熟悉Dart和Flatter,我正在尝试使用Flatter和Firebase(因此,Firestore作为数据库)构建一个简单的应用程序 以下是我努力实现的目标: 我希望学生能够选择一所学校 选择后,我将学生的学校更新为学校的documentID引用(一些由Firestore自动生成的UID)。示例:学校:“eggdjg2jkfwillfgvhpee”。我也有一个电子邮件和电话号码附加到学生 就这样 问题是:学校价值观在Firestore上不断更新。我现在有两个学校,它在这两个值之间

我不熟悉Dart和Flatter,我正在尝试使用Flatter和Firebase(因此,Firestore作为数据库)构建一个简单的应用程序

以下是我努力实现的目标:


  • 我希望学生能够选择一所学校
  • 选择后,我将学生的学校更新为学校的documentID引用(一些由Firestore自动生成的UID)。示例:学校:“eggdjg2jkfwillfgvhpee”。我也有一个电子邮件和电话号码附加到学生
  • 就这样

问题是:学校价值观在Firestore上不断更新。我现在有两个学校,它在这两个值之间切换(+它使用了很多Firebase自由层编写操作)

最后但并非最不重要的一点是,此算法位于“选择你的学校”页面上,但如果我重新加载应用程序,它将在我转到此页面时自动触发changeSchool功能(请参见下文)(无需点击列表互动程序!),但这不是我卸载或重新安装后第一次启动应用程序。。(如果不清楚,请告诉我,我已经在那里呆了几个小时)

未来变更学校(学校)异步{
收藏参考学校收藏=
Firestore.instance.collection('School');
集合参考学生集合=
Firestore.instance.collection('Student');
校藏
.where('name',isEqualTo:school.name)
.where('city',isEqualTo:school.city)
.where('country',isEqualTo:school.country)
.快照()
.listen((数据){
if(data.documents.length==1){
学生收藏
.where('email',isEqualTo:globals.currentUser.email)
.快照()
.listen((学生)异步{
最终文件参考研究中心=
studentCollection.document(students.documents[0].documentID);
最终文件参考=
schoolCollection.document(data.documents[0].documentID);
globals.currentSchool=School.fromSnapshot(wait ref.get());
Student tmpStudent=Student.fromSnapshot(wait studentRef.get());
tmpStudent.school=ref.documentID;
Firestore.instance.runTransaction((事务)异步{
wait transaction.update(studentRef,tmpStudent.toJson());
});
});
}
});
下面是触发此函数的内容:

child:ListTile(
标题:文本(学校名称),
尾随:文本(school.studentsNumber),
onTap:()异步{
等待更换学校(学校);
Navigator.pop(上下文);
},
),
因为我是新手,如果有更好的方法,我也很开放


谢谢!

因此,我可以使用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());
  });
}

未来变更学校(学校)异步{
收藏参考学校收藏=
Firestore.instance.collection('School');
集合参考学生集合=
Firestore.instance.collection('Student');
QuerySnapshot schoolQuery=等待学校收集
.where('name',isEqualTo:school.name)
.where('city',isEqualTo:school.city)
.where('country',isEqualTo:school.country)
.getDocuments();
QuerySnapshot studentQuery=等待studentCollection
.where('email',isEqualTo:globals.currentUser.email)
.getDocuments();
最终文件参考研究中心=
studentCollection.document(studentQuery.documents[0].documentID);
最终文件参考=
schoolCollection.document(schoolQuery.documents[0].documentID);
globals.currentSchool=School.fromSnapshot(wait ref.get());
Student tmpStudent=Student.fromSnapshot(wait studentRef.get());
tmpStudent.school=ref.documentID;
Firestore.instance.runTransaction((事务)异步{
wait transaction.update(studentRef,tmpStudent.toJson());
});
}