[Custom Function] Random List Of DocumentReference

Hello,

A custom fonction to return randomly an input list of DocumentReference and retunring only a number of desired Document Reference.

Input : List DocumentReference for your main query
Input : number Of Document the list output

Output : A Random List Of DocumentReference









The Code :

import 'dart:math' as math;

List<DocumentReference> randomID(
  List<DocumentReference> listdocument,
  int numberOfDocument,
) {
  List<int> indices = List<int>.generate(listdocument.length, (i) => i);

  indices.shuffle();

  int newCount = numberOfDocument;

  List<DocumentReference> randomList =
      indices.take(newCount).map((i) => listdocument[i]).toList();

  return randomList;
}


Set a General Query on your page ( scaffold )

Add Column or Row for generating Dynamic Child from the custom Function

Add a Container to Query from your Document Child of the Column / Row

Add any widget to set desired field from the Container Query

The Cost will be all the reads from the main Query [ you can add filter or limits here ] + The cost of the number of documents you choose to output from the Custom Fonction+ The cost of the number you choose to output from the Query at the container level. 

The best solution will be to implement it direclty on the Firebsase Backend with a Function. 



Hope it will be useful for some

4
19 replies