randomEntryByField method

String randomEntryByField(
  1. String field
)

Retrieves a random value from a random entry in the Map based on the specified field.

The field parameter represents the field/key in the entry from which the random value should be retrieved. The function generates a random index within the range of the Map's length using a Random generator. It retrieves a random entry using the generated index and then retrieves the value associated with the specified field.

Returns a string representation of the random value.

Example:

Map<String, dynamic> data = {
  'entry1': {'name': 'Alice', 'age': 25},
  'entry2': {'name': 'Bob', 'age': 30},
  'entry3': {'name': 'Charlie', 'age': 35},
};

String randomName = data.randomEntryByField('name');
print(randomName); // Output: Alice (or Bob, or Charlie)

Implementation

String randomEntryByField(String field) {
  Random generator = Random();
  int index = generator.nextInt(this.length);
  MapEntry entry = this.entries.toList()[index];
  return entry.value[field].toString();
}