简体   繁体   English

Flutter 在呈现数据的页面上持续轮询新数据的最佳实践?

[英]Flutter Best Practices to Continuously Poll for New Data while on a Page which renders that Data?

I have an App written in Flutter, and I wish to have the App immediately render new Objects found in the Database, but the best way I can think of achieving this is firing a GET request to the Backend Database and call "setState()" every 5~10 seconds.我有一个用 Flutter 编写的应用程序,我希望该应用程序立即呈现在数据库中找到的新对象,但我能想到的最佳方法是向后端数据库发出 GET 请求并调用“setState()”每 5~10 秒。 This is so that when a user is in his "Order" table in-phone, and places a new "Order" from his/her laptop, I want that "Object" to show up in that users table in phone, without any page navigations or reloading the page.这样,当用户在手机中的“订单”表中,并从他/她的笔记本电脑放置新的“订单”时,我希望该“对象”显示在手机中的用户表中,没有任何页面导航或重新加载页面。

The Table is implemented as a ListView.Builder()该表被实现为一个ListView.Builder()

If you manage also the backend I think one way could be to use websocket.如果您还管理后端,我认为一种方法是使用 websocket。 When an order is inserted you call an API that emits an event to a websocket, your Flutter app that uses websocket can listen for this event and eventually rebuild the data.当插入订单时,您调用 API 向 websocket 发出事件,使用 websocket 的 Flutter 应用程序可以侦听此事件并最终重建数据。 In this way you don't need to call the GET api every N seconds but the server "calls you".通过这种方式,您不需要每 N 秒调用一次 GET api,而是服务器“呼叫您”。 Here some flutter documentation about websockets: https://flutter.dev/docs/cookbook/networking/web-sockets这里有一些关于 websockets 的 flutter 文档: https ://flutter.dev/docs/cookbook/networking/web-sockets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM