In my app, the user will fill out a form, and then Firebase will send them a random Document after 10 minutes. I am not entirely sure how to do this and was wondering if someone could point me in the right direction.
Thank you for your kind support
In my app, the user will fill out a form, and then Firebase will send them a random Document after 10 minutes. I am not entirely sure how to do this and was wondering if someone could point me in the right direction.
Thank you for your kind support
Unfortunately, there is no super simple way to do this. The best way to do this would be to pull all the documents from your collection, shuffle them, then retrieve the first document. Example code:
//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