最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fetch data (method call) every five seconds in Flutter? - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Jun 1, 2019 at 8:42 halfer 20.3k19 gold badges109 silver badges202 bronze badges asked Jun 24, 2018 at 1:42 ctrinhctrinh 4251 gold badge6 silver badges14 bronze badges 3
  • 2 Possible duplicate of How do I run a reoccurring function, in Dart? – Saikrishna Rajaraman Commented Jun 24, 2018 at 4:17
  • 1 For flutter you can try with Stream and Stream Builder. I think this post will help you a lot. dev.to/nitishk72/understanding-streams-in-flutter-dart-2pb8 – Ye Yint Commented Jun 1, 2019 at 6:54
  • 1 An answer below has provided this link; I am preserving it here in case it is deleted. – halfer Commented Jun 1, 2019 at 8:39
Add a ment  | 

2 Answers 2

Reset to default 7

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());
}

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.
});
发布评论

评论列表(0)

  1. 暂无评论