checkNumericOnly method

bool checkNumericOnly(
  1. String? data
)

Implementation

bool checkNumericOnly(String? data) {
  //This function takes a string of data and breaks it into parts and trys to do Int64.TryParse
  //This will verify that only numeric data is contained in the string passed in.  The complexity below
  //was done to ensure that the minimum number of interations and checks could be performed.

  //early check to see if the whole number can be parsed to improve efficency of this method

  if (data != null) {
    if (int.tryParse(data) != null) return true;
  } else {
    return false;
  }

  //9223372036854775808 is the largest number a 64bit number(signed) can hold so ... make sure its less than that by one place
  const int stringLengths = 18;

  var temp = data;
  var list = List<String>.filled((data.length ~/ stringLengths) +
      ((data.length % stringLengths == 0) ? 0 : 1), '');
  var strings = list;

  var i = 0;

  while (i < strings.length) {
    if (temp.length >= stringLengths) {
      strings[i++] = temp.substring(0, stringLengths);
      temp = temp.substring(stringLengths);
    } else {
      strings[i++] = temp.substring(0);
    }
  }

  return !strings.any((s) => int.tryParse(s) == null);
}