当用户被签名并重新签名时,Firestore听众停止聆听

发布于 2025-02-03 15:57:39 字数 848 浏览 5 评论 0原文

最小可重复的代码

@override
void initState() {
  super.initState();
  
  final col = FirebaseFirestore.instance.collection('foo');
  col.doc('bar').snapshots().listen((event) {
    print('Data = ${event.data()}');
  });
}
  
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Column(
      children: [
        ElevatedButton(
          onPressed: () => FirebaseAuth.instance.signInAnonymously(),
          child: Text('Sign in'),
        ),
        ElevatedButton(
          onPressed: () => FirebaseAuth.instance.signOut(),
          child: Text('Sign out'),
        ),
      ],
    ),
  );
}

我的安全规则是:

allow read, write: if request.auth != null;

当我再次登录并再次登录时,我的听众停止聆听。这是设计吗?每次都有一个签名活动时,我是否应该再次设置听众?

Minimum reproducible code

@override
void initState() {
  super.initState();
  
  final col = FirebaseFirestore.instance.collection('foo');
  col.doc('bar').snapshots().listen((event) {
    print('Data = ${event.data()}');
  });
}
  
@override
Widget build(BuildContext context) {
  return Scaffold(
    body: Column(
      children: [
        ElevatedButton(
          onPressed: () => FirebaseAuth.instance.signInAnonymously(),
          child: Text('Sign in'),
        ),
        ElevatedButton(
          onPressed: () => FirebaseAuth.instance.signOut(),
          child: Text('Sign out'),
        ),
      ],
    ),
  );
}

My security rules are:

allow read, write: if request.auth != null;

When I sign out and sign in again, my listener stops listening. Is this by design? Should I again setup my listener each time there's a sign out event?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-02-10 15:57:39

是的,您看到的是预期的行为。

由于您的安全规则要求在用户中签名以读取数据,因此签名当前用户取消了任何现有的侦听器(因为它们不再满足要求)。

一旦完成登录,您将必须(重新)为听众附加侦听器。

Yes, what you're seeing is the expected behavior.

Since your security rules require that there is a signed in user to read the data, signing out the current user cancels any existing listeners (as they no longer meet the requirement).

You will have to (re)attach listeners for the new user once their sign in is completed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文