首页 > 解决方案 > 在设定的时间段后从 Firebase 检索随机数据

问题描述

在我的应用程序中,用户将填写一份表格,然后 Firebase 将在 10 分钟后向他们发送一个随机文档。我不完全确定如何做到这一点,并想知道是否有人能指出我正确的方向。

感谢您的支持

标签: firebaseflutter

解决方案


不幸的是,没有超级简单的方法可以做到这一点。最好的方法是从您的集合中提取所有文档,将它们打乱,然后检索第一个文档。示例代码:

//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

推荐阅读