Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading