Sometimes we need to show a updated value which is coming from the server. In that case we need to fetch data repeatedly with interval of 5-10 seconds or more.

Let’s see how set a timer :

void _startAutoRefresh() {
    _timer = Timer.periodic(Duration(seconds: 10), (timer) {
        // Write your code which you want to execute repeatedly with the interval of 10 seconds.
    });
}

You can set 5 in place of 10, which means it will run again again with a gap of 5 seconds. Like below
Timer.periodic(Duration(seconds: 5)
This code will run repeatedly until you cancel it.

Let’s see how to cancel the timer :

@override
void dispose() {
    _timer?.cancel(); // Stops the timer
    super.dispose();
}

When you go back to previous page using pop() method then this dispose() function is called automatically. But in others cases you need to call it manually.