isValidCnpj static method
Validates the check digits of a 14-digit CNPJ.
Implementation
static bool isValidCnpj(String cnpj) {
if (cnpj.length != 14) return false;
if (RegExp(r'^(\d)\1{13}$').hasMatch(cnpj)) return false;
int checkDigit(List<int> weights) {
var sum = 0;
for (var i = 0; i < weights.length; i++) {
sum += int.parse(cnpj[i]) * weights[i];
}
final rest = sum % 11;
return rest < 2 ? 0 : 11 - rest;
}
const weights1 = [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
const weights2 = [6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2];
return checkDigit(weights1) == int.parse(cnpj[12]) &&
checkDigit(weights2) == int.parse(cnpj[13]);
}