Home > other >  How to subscribe to the response that returns from http request in Flutter?
How to subscribe to the response that returns from http request in Flutter?

Time:03-14

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.

CodePudding user response:

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

  • Related