firebase - 在设定的时间段后从 Firebase 检索随机数据
问题描述
在我的应用程序中,用户将填写一份表格,然后 Firebase 将在 10 分钟后向他们发送一个随机文档。我不完全确定如何做到这一点,并想知道是否有人能指出我正确的方向。
感谢您的支持
解决方案
不幸的是,没有超级简单的方法可以做到这一点。最好的方法是从您的集合中提取所有文档,将它们打乱,然后检索第一个文档。示例代码:
//get firestore documents from collection
QuerySnapshot qs = await Firestore.instance.collection('myCollection').getDocuments();
List<DocumentSnapshot> listedQS = qs.documents; //listed documents
var random = new Random(); //dart math
//shuffle the array
for (var i = listedQS.length - 1; i > 0; i--) {
var n = random.nextInt(i + 1);
var temp = listedQS[i];
listedQS[i] = listedQS[n];
listedQS[n] = temp;
}
DocumentSnapshot randomDocument = listedQS[0]; //the random data from firebase
推荐阅读
- model-view-controller - MVC Core 2.2 找不到 MaxJsonDeserializerMembers
- viber - Viber 不断将点击的按钮 url 放入对话中
- python - 我在为模板渲染动态数据时遇到问题
- sql - 无法将值插入表(Oracle)
- intersystems-cache - 有谁知道 JSON 解码器 für Cache 2008 (native Cache)
- html - 输入 [type=file] 文本上传文件,我面临上传文件显示问题
- bash - 命令挂载到 crontab 在 macOS 中不起作用
- laravel - 将 laravel 与子目录中的另一个应用程序一起使用
- swift - 使用 Azure Facebook 身份验证时未调用 SFAuthenticationSession 完成处理程序
- gcloud - 错误:(gcloud.compute.scp)[/usr/bin/scp] 退出并返回代码 [1]