generate static method

Cpf generate([
  1. int? seed
])

Implementation

static Cpf generate([int? seed]) {
  final randomGenerator = Random(seed);
  final digits = [for (var i = 1; i <= 9; i++) randomGenerator.nextInt(10)];
  final sum1 = ([
            for (var i = 1; i <= 9; i++) digits.elementAt(i - 1) * (11 - i)
          ].fold(0, (x, y) => x + y) *
          10) %
      11;
  final sum2 = ([
            for (var i = 1; i <= 10; i++)
              [...digits, sum1 == 10 || sum1 == 11 ? 0 : sum1]
                      .elementAt(i - 1) *
                  (12 - i)
          ].fold(0, (x, y) => x + y) *
          10) %
      11;
  final dv1 = sum1 == 10 || sum1 == 11 ? 0 : sum1;
  final dv2 = sum2 == 10 || sum2 == 11 ? 0 : sum2;

  return Cpf._([...digits, dv1, dv2].map((d) => d.toString()).join(''));
}