getSize method

String getSize(
  1. dynamic size, {
  2. PrecisionValue precisionValue = PrecisionValue.Two,
})

size can be passed as number or as string the optional parameter PrecisionValue specifies the number of digits after comma/point (default is PrecisionValue.Two)

Example:

   FileSize.getSize(1024)
   FileSize.getSize(1024, precisionValue = PrecisionValue.Four)

Implementation

String getSize(dynamic size,
    {PrecisionValue precisionValue = PrecisionValue.Two}) {
  int divider = 1024;
  int _size;
  this._precisionValue = precisionValue;

  try {
    _size = size is int ? size : int.parse(size.toString());
  } on FormatException catch (e) {
    throw FormatException("Can not parse the size parameter: ${e.message}");
  }

  if (_size < divider) return getBytes(_size);
  if (_size < _getDividerValue(2)) return getKiloBytes(_size);
  if (_size < _getDividerValue(3)) return getMegaBytes(_size);
  if (_size < _getDividerValue(4)) return getGigaBytes(_size);
  if (_size < _getDividerValue(5)) return getTeraBytes(_size);
  if (_size < _getDividerValue(6)) return getPetaBytes(_size);
  if (_size < _getDividerValue(7)) return getExaBytes(_size);
  if (_size < _getDividerValue(8)) return getYottaBytes(_size);
  if (_size < _getDividerValue(9)) return getZettaBytes(_size);
  return getZettaBytes(_size);
}