generateIdioms function Null safety

Iterable<String> generateIdioms(
  1. {int top = topDefault,
  2. Random? random}
)

Randomly generates Chinese idioms. You can inject Random using the random parameter.

Implementation

Iterable<String> generateIdioms({int top = topDefault, Random? random}) sync* {
  final rand = random ?? _random;
  var idioms = all.take(top).toList(growable: false);
  String pickRandom(List<String> list) => list[rand.nextInt(list.length)];

  // We're in a sync* function, so `while (true)` is okay.
  while (true) {
    final String idiom = pickRandom(idioms);

    yield idiom;
  }
}