Dart 颤动流停止和启动有条件吗?

发布于 2025-01-12 04:26:32 字数 225 浏览 2 评论 0原文

我想有条件地停止并重新启动流,我该怎么做?

_fireStore
.collection(...)
.doc(...)
.snapshots().takeWhile((element) => _listenable);

_listenable 为 false 时流停止,但当 true 时不重新启动?

有人知道该怎么做吗?

I want to stop and restart a stream conditionally, how can I do that?

_fireStore
.collection(...)
.doc(...)
.snapshots().takeWhile((element) => _listenable);

Stream stops when _listenable is false but doesn't restart when true ?

Anyone know how to do it?

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

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

发布评论

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

评论(1

卷耳 2025-01-19 04:26:32

您可以使用 async 轻松控制流 这就是您可以做到的。

首先像这样创建一个 StreamSubsctiprion

StreamSubscription<QuerySnapshot>? _eventStream;

,然后为快照流设置一个侦听器。

这是如何存储流查询快照

 Stream<QuerySnapshot> streamSub =_fireStore
    .collection(...)
    .doc(...)
    .snapshots();

为您的流添加侦听器,如下所示

   _eventStream = streamSub.listen((snapshot) => _eventStream);

然后您可以使用事件流取消、暂停和恢复您的流,如下所示这

 _eventStream!.pause();
 _eventStream!.cancel();
 _eventStream!.resume();

是我的最终代码

class MainScreen extends State<PaymentScreen> {
  StreamSubscription<QuerySnapshot>? _eventStream;

  @override
  Widget build(BuildContext context) {
    Stream<QuerySnapshot> myStream = FirebaseFirestore.instance.collection('Users').snapshots();
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            StreamBuilder<QuerySnapshot>(
              stream: myStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }

                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }

                return Column(
                  children:
                      snapshot.data!.docs.map((DocumentSnapshot document) {
                    Map<String, dynamic> data =
                        document.data()! as Map<String, dynamic>;
                    return ListTile(
                      title: Text(data['firstName']),
                    );
                  }).toList(),
                );
              },
            ),
            FloatingActionButton.extended(
              onPressed: () {
                _eventStream = myStream.listen((snapshot) => _eventStream);
                try {
                  _eventStream!.pause();
                  print('paused');
                } catch (e, s) {
                  print(s);
                }
              },
              label: const Text("Pause Stream"),
            ),
          ],
        ),
      ),
    );
  }
}

You can use async to easily control the stream This is how you can do it.

First create a StreamSubsctiprion like this

StreamSubscription<QuerySnapshot>? _eventStream;

and then set a listener for your snapshots stream.

here is how you can store stream query snapshot

 Stream<QuerySnapshot> streamSub =_fireStore
    .collection(...)
    .doc(...)
    .snapshots();

add listener for your stream like this

   _eventStream = streamSub.listen((snapshot) => _eventStream);

Then you can use your event stream to cancel, pause and resume your stream like this

 _eventStream!.pause();
 _eventStream!.cancel();
 _eventStream!.resume();

here is my final code

class MainScreen extends State<PaymentScreen> {
  StreamSubscription<QuerySnapshot>? _eventStream;

  @override
  Widget build(BuildContext context) {
    Stream<QuerySnapshot> myStream = FirebaseFirestore.instance.collection('Users').snapshots();
    return Scaffold(
      body: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            StreamBuilder<QuerySnapshot>(
              stream: myStream,
              builder: (BuildContext context,
                  AsyncSnapshot<QuerySnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text('Something went wrong');
                }

                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Text("Loading");
                }

                return Column(
                  children:
                      snapshot.data!.docs.map((DocumentSnapshot document) {
                    Map<String, dynamic> data =
                        document.data()! as Map<String, dynamic>;
                    return ListTile(
                      title: Text(data['firstName']),
                    );
                  }).toList(),
                );
              },
            ),
            FloatingActionButton.extended(
              onPressed: () {
                _eventStream = myStream.listen((snapshot) => _eventStream);
                try {
                  _eventStream!.pause();
                  print('paused');
                } catch (e, s) {
                  print(s);
                }
              },
              label: const Text("Pause Stream"),
            ),
          ],
        ),
      ),
    );
  }
}

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