Reputation: 425
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?
Upvotes: 4
Views: 7728
Reputation: 56
Timer() and Timer.periodic() work the same way. They take duration as a parameter and an optional callback function.
Timer(const Duration(seconds: 5), () {
// these lines would be executed every 5s.
});
Upvotes: 0
Reputation: 10759
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());
}
Upvotes: 7