首页 > 解决方案 > 如何创建不消耗内存的轮询?

问题描述

我正在尝试创建对 API 的长时间轮询,该 API 每五秒将数据提取到应用程序的主页,该主页处于信息亭模式。因此,我添加了以下代码:

Timer.periodic(Duration(seconds: 5), (timer) async {
   // code that polls for the active users from server
   final response = await http.get(_url);
   // rest of the code (which can be ignored)
});

这很好用,但是当我在颤振工具中跟踪配置文件模式时,我发现内存使用量及时增加。一两天后,应用程序甚至会因为内存不足异常而崩溃。如果有任何最佳实践可以在没有内存崩溃或清除的情况下执行此操作,我将不胜感激。

谢谢

标签: flutterdartout-of-memory

解决方案


推荐阅读