I have temperature sensor and I am getting the current temperature data from the API. What I want to do is updating the temperature value in the screen whenever it changes. To do that do I have to send request periodically or is it possible to subscribe to API ? Thanks in advance.
1 Answer
That would depend on your API. If it can keep sending data when it changes, then sure you can use a StreamBuilder
to rebuild on a changed state. Otherwise, you'll probably have to deal with a StreamBuilder
with a Stream.periodic
that sends the request every so many seconds (and still only rebuilds when the data has actually changed, of course).
https://api.flutter.dev/flutter/dart-async/Stream/Stream.periodic.html
-
Thank you for your answer. Looks like Stream.periodic is what I'm looking for Mar 13, 2022 at 18:46