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

Viewed 670

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. 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()

1 Answers

If you manage also the backend I think one way could be to use 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. In this way you don't need to call the GET api every N seconds but the server "calls you". Here some flutter documentation about websockets: https://flutter.dev/docs/cookbook/networking/web-sockets

Related