如何显示所有用户的倒数时间 [英] How to display the countdown time for all users

查看:78
本文介绍了如何显示所有用户的倒数时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个颤动的拍卖应用程序,我被困在拍卖产品的倒计时时间,所以我使用Firebase实时数据库.我必须确保所有用户都同时看到倒数计时.无论是搜索关键字,类似示例还是对我有帮助的内容,请帮助我.而且我可以使用nodejs进行协助,

I am building a flutter auction application, I am stuck at the countdown time of an auction product so I use firebase realtime database. I have to make sure all users see the countdown at the same time. Please help me whether it's just a keyword to search for, a similar example or something that can help me. And I can use nodejs to assist,

这是我的数据库,我想要当用户发布他们的产品时,倒数计时,然后获取该数据以在屏幕上显示它.我在哪里放置倒数计时器?在扑火或火力中.我在哭谢谢你所做的一切,

This is my database, I want when a user posts a product of theirs, the time countdown, and I get that data to display it on the screen. Where do I put that countdown timer? In flutter or firebase. I'm crying. Thank you for everything,

推荐答案

一个简单的解决方案是设置拍卖的结束时间 DateTime .例如,如果您希望两个小时后终止拍卖,则可以添加

One simple solution is to set the ending DateTime of your auction. For example, if you want your auction to be terminated after two hours, you can add

endingTime = DateTime.now().add(Duration(hours: 2))

到Firebase实时数据库,然后当用户参加拍卖时,您可以使用 Timer.periodic 计算每1秒钟的剩余时间,并在当前 DateTime 超出了终止时间:

to Firebase realtime database, and then when users come to your auction, you can just use a Timer.periodic to calculate the remaining time for every 1 second, and stop the timer when the current DateTime exceeds the termination time:

Timer.periodic(Duration(seconds: 1), (timer) {
  remainingTime = endingTime.difference(DateTime.now());
  if (remainingTime <= Duration.zero) {
     timer.cancel()
  }
});

最后,您可以只使用 setState 渲染 remainingTime ,或使用 StreamBuilder 实现 StreamController 它.希望对您有所帮助.

And finally, you can just use setState to render remainingTime, or implement StreamController with StreamBuilder to render it. Hope it helps.

这篇关于如何显示所有用户的倒数时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆