4

In JavaScript, I could constantly fetch data without an explicit request from the user by calling a function fetchData() every five seconds using setInterval(function() { fetchData() }, 5000); and this is an incredibly useful tool for me. Is there a similar equivalent in Flutter?

halfer
  • 19,471
  • 17
  • 87
  • 173
ctrinh
  • 335
  • 1
  • 4
  • 13
  • 2
    Possible duplicate of [How do I run a reoccurring function, in Dart?](https://stackoverflow.com/questions/14946012/how-do-i-run-a-reoccurring-function-in-dart) – Saikrishna Rajaraman Jun 24 '18 at 04:17
  • 1
    An answer below has provided [this link](https://dev.to/nitishk72/understanding-streams-in-flutter-dart-2pb8); I am preserving it here in case it is deleted. – halfer Jun 01 '19 at 08:39
  • 1
    For flutter you can try with Stream and Stream Builder. I think this post will help you a lot. https://dev.to/nitishk72/understanding-streams-in-flutter-dart-2pb8 – ye yint Jun 01 '19 at 06:54

1 Answers1

6

This can be achieved by something like this.

import 'dart:async';

main() {
  const fiveSeconds = const Duration(seconds: 5);
  // _fetchData() is your function to fetch data
  Timer.periodic(fiveSeconds, (Timer t) => _fetchData());
}
Community
  • 1
  • 1
Jaswant Singh
  • 8,387
  • 7
  • 25
  • 47