Flutter:如何使我的流不必重新加载页面显示更新的值?

发布于 2025-01-26 08:02:00 字数 1823 浏览 4 评论 0原文

我有用户在几秒钟内从Firebase上花费的时间,然后将其转换为小时,最小值和SEC,并希望向用户显示,我使用的是流,但是当有新值时,它不会更新。我必须重新加载页面或转到其他页面,然后回来查看更新的值。我在做什么错?

late Stream<String> testStream;

  @override
  void initState() {
    super.initState();
      setState(() {
        testStream = printDuration();
      });
    });    
  }
  

  Stream<String>printDuration() async*{
   FirebaseFirestore.instance
        .collection("users")
        .doc(user!.uid)
        .snapshots()
        .listen(
          (event) => print("current data: ${event.data()!['timeActive']}"),
          onError: (error) => print("Listen failed: $error"),
        );

    Duration duration = Duration(seconds: loggedInUser.timeActive!);
    String? data = loggedInUser.timeActive.toString();
     print('###');
    print(data);
    String twoDigits(int n) => n.toString().padLeft(2, "0");
    String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
    String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
    String _getData = "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
    print(_getData);
        yield _getData; 
}

StreamBuilder<String>(
  stream: testStream,
  builder: ((context, snapshot) {
    if (snapshot.data != null) {
      //userDocument['timeActive'];
      return Center(
        child: Text(snapshot.data,
          style: TextStyle(
            color: Color(0xff516395),
            fontSize: 26.0,
            letterSpacing: 2.0,
            fontWeight: FontWeight.bold,
          ),
        ),
      );
    } else {
      return Text('0',
          style: TextStyle(
            color: Color(0xff516395),
            fontSize: 30.0,
            letterSpacing: 2.0,
            fontWeight: FontWeight.bold,
          ));
    }
  }),
)

I have user time spent on app from firebase in seconds, I then convert it into hours, min and sec, and want to display to the user, I'm using streams but it will not update when there is a new value. I have to reload the page or go to a different page then come back to see the updated value. What am I doing wrong?

late Stream<String> testStream;

  @override
  void initState() {
    super.initState();
      setState(() {
        testStream = printDuration();
      });
    });    
  }
  

  Stream<String>printDuration() async*{
   FirebaseFirestore.instance
        .collection("users")
        .doc(user!.uid)
        .snapshots()
        .listen(
          (event) => print("current data: ${event.data()!['timeActive']}"),
          onError: (error) => print("Listen failed: $error"),
        );

    Duration duration = Duration(seconds: loggedInUser.timeActive!);
    String? data = loggedInUser.timeActive.toString();
     print('###');
    print(data);
    String twoDigits(int n) => n.toString().padLeft(2, "0");
    String twoDigitMinutes = twoDigits(duration.inMinutes.remainder(60));
    String twoDigitSeconds = twoDigits(duration.inSeconds.remainder(60));
    String _getData = "${twoDigits(duration.inHours)}:$twoDigitMinutes:$twoDigitSeconds";
    print(_getData);
        yield _getData; 
}

StreamBuilder<String>(
  stream: testStream,
  builder: ((context, snapshot) {
    if (snapshot.data != null) {
      //userDocument['timeActive'];
      return Center(
        child: Text(snapshot.data,
          style: TextStyle(
            color: Color(0xff516395),
            fontSize: 26.0,
            letterSpacing: 2.0,
            fontWeight: FontWeight.bold,
          ),
        ),
      );
    } else {
      return Text('0',
          style: TextStyle(
            color: Color(0xff516395),
            fontSize: 30.0,
            letterSpacing: 2.0,
            fontWeight: FontWeight.bold,
          ));
    }
  }),
)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文