flutter - 如何在颤动中限制onLocationChanged
问题描述
我想每 10 分钟更新一次用户的位置。工作正常,onLocationChanged
即使应用程序在后台。但是,当位置更改时,它会不断抛出位置详细信息。我希望它_getCurrentLocationtoServer()
在 10 分钟后运行一个方法。
location.onLocationChanged.listen((LocationData cLoc) {
currentLocation = cLoc;
_getCurrentLocationtoServer();
});
void _getCurrentLocationtoServer() async{
//code to send to server
}
解决方案
不要在 onLocationChanged 中调用 _getCurrentLocationtoServer()。只需分配 currentLocation 值。现在,做一个新的
Timer.periodic(Duration(minutes: 10), () => _getCurrentLocationtoServer());
这会定期运行,并每 10 分钟向服务器发送一次 currentLocation 值。在将 currentLocation 值发送到服务器之前,请确保它不为空。
推荐阅读
- angular - jhipster 和当前日期显示和检查
- scala - 如何针对 Long 值类型优化我的 properFractions 函数
- tinymce - 从 TinyMCE 获取 HTML 标记?
- java - 替换 Jenkins 的扩展选择或复选框的活动选择分隔符
- python-3.x - HTTPError:HTTP 错误 403:在从 Python3 中的链接下载 csv 文件期间定义标头时返回 Forbidden 或 None
- azure - 启用对 API 的 Azure 可用性测试访问
- python - 如何对推文进行分类(支持与不支持)以预测选举结果
- google-cloud-firestore - 设置文档失败时,如何在我的 firebase 函数中发现错误?
- linux - 最大化 linux 原始套接字吞吐量
- amazon-web-services - 是否可以在 2 个存储桶之间复制特定的 S3 文件夹?