Version constructor

Version(
  1. int major,
  2. int minor,
  3. int patch, {
  4. List<String> preRelease = const <String>[],
  5. String build = "",
})

Creates a new instance of Version.

major, minor, and patch are all required, all must be greater than 0 and not null, and at least one must be greater than 0. preRelease is optional, but if specified must be a List of String and must not be null. Each element in the list represents one of the period-separated segments of the pre-release information, and may only contain 0-9A-Za-z-. build is optional, but if specified must be a String. must contain only 0-9A-Za-z-., and must not be null. Throws a FormatException if the String content does not follow the character constraints defined above. Throes an ArgumentError if any of the other conditions are violated.

Implementation

Version(this.major, this.minor, this.patch,
    {List<String> preRelease = const <String>[], this.build = ""})
    : _preRelease = preRelease {
  for (int i = 0; i < _preRelease.length; i++) {
    if (_preRelease[i].toString().trim().isEmpty) {
      throw ArgumentError("preRelease segments must not be null or empty");
    }
    // Just in case
    _preRelease[i] = _preRelease[i].toString();
    if (!_preReleaseRegex.hasMatch(_preRelease[i])) {
      throw FormatException(
          "preRelease segments must only contain [0-9A-Za-z-]");
    }
  }
  if (this.build.isNotEmpty && !_buildRegex.hasMatch(this.build)) {
    throw FormatException("build must only contain [0-9A-Za-z-.]");
  }

  if (major < 0 || minor < 0 || patch < 0) {
    throw ArgumentError("Version numbers must be greater than 0");
  }
}