firebase - 先 flutter 不响应的地方(firestore)

标签 firebase flutter dart stream listen

几个月前,我做了一个功能,我的应用程序正在等待用户文档并做出相应的响应。直到我对项目进行优化并将其更新到最新版本之前,它一直是一种魅力。
如果存在用户文档,则流将产生该文档并关闭该流。
如果云Firestore中没有用户数据,则该流将产生null并等待文档出现在云中。

// this function doesn't work properly and it should work but `firstWhere` is not
// listening to the stream unless there is a listener already wich makes no sense

static Stream<DocumentSnapshot> get getUserDocumentWhenExists async* {
  User user = FirebaseAuth.instance.currentUser;
  if (user == null) throw 'No user is signed in';

  FirebaseFirestore firebase = FirebaseFirestore.instance;
  CollectionReference usersCollection = firebase.collection('users');
  Stream<DocumentSnapshot> userDocumentStream = usersCollection.doc(user.uid).snapshots();

  userDocumentStream.listen((event) {}); // <= this is here for the code to work

  DocumentSnapshot userDocument = await userDocumentStream.first;
  if (userDocument.exists == false) {
    yield null;
    yield await userDocumentStream.firstWhere((userDocument) {
      // not beeing called without a previous listener
      return userDocument.exists;
    });
  } else {
    yield userDocument;
  }
}
如果您运行此代码时未删除userDocumentStream.listen((event) {}),则它将像更新前一样正常工作。
我的问题是:
这是一个错误吗?
为什么会这样呢?还是我写错了什么?
编辑:我进行了不带Firebase的自定义测试,并且一切正常。只是在这种特殊情况下,firstWhere()没有收听流
Edit2 :经过更多测试后,我发现userDocumentStream.first之后的所有侦听器均不起作用。现在我更困惑了,我真的需要一些帮助

最佳答案

我认为在调用first()之后,即使在first文档中另有说明,订阅也会被取消:
“在内部,该方法在第一个元素之后取消其订阅。这意味着单订阅(非广播)流已关闭,并且在调用此getter之后不能重用。”
我的解决方案:
snapshots()创建一个first()流,为firstWhere()创建一个

final documentReference = usersCollection.doc(user.uid);

final userDocument = await documentReference.snapshots().first;
if (userDocument.exists == false) {
  yield null;
  yield await documentReference.snapshots().firstWhere((userDocument) {
    return userDocument.exists;
  });
} else {
  yield userDocument;
}

关于firebase - 先 flutter 不响应的地方(firestore),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64271313/

相关文章:

firebase - Google Firestore - 是否存在小型业务?

flutter - 使用参数调用的方法与不使用参数的方法之间的区别

Flutter 文本字段允许用户仅在给定范围内插入数字

flutter - 是否可以将 SliverGrid (是 CustomScrollView 的子级)包装在 OrientationBuilder 中?

dart - 在Polymer中触发CustomEvent给我 “Exception: Class ' MyData'没有实例方法 '[]'”

swift - 在 Firebase 数据库中按用户名搜索用户?

firebase - AngularFire 访问子元素方法

javascript - 使用 firebase 数据库访问函数返回值

flutter - 任务 ':app:mergeDexDebug' 执行失败。火店| flutter

dart - 我想过滤对象列表取决于日期