title: Flutter Parse Method Explanation
body: <p>I have a String date in format Month-Day-4DigitYear that I want to convert to DateTime in Flutter. I'm a novice coder, and I'm struggling to understand the <a href="https://api.flutter.dev/flutter/dart-core/DateTime/parse.html" rel="nofollow noreferrer">api.flutter.dev Parse method example.</a></p>
  <p>Below is the example.  I just have a few issues.  Android Studio throws multiple errors when I just create a class and put in this function.  I think I understand the non-nullable issue, so I delete the ! and ? marks everywhere.</p>
  <p>My issues are: what are _parseFormat, _brokenDownDateToValue, _withValue ?</p>
  <p>All give errors and just declaring the first two and deleting the _withValue doesn't seem to do the trick, although removes all errors.  It's like they've left out a key portion that I'm missing or there is a package I need to import the neither I nor Android Studio knows about.  Can anyone decrypt this?  I get very frustrated with flutter's documentation, as it always seems to give 80% of required info, assuming you already are clairvoyant on all other topics except this single one they are discussing.  Gotta be a pro before reading the manual.</p>
  <pre><code>    // TODO(lrn): restrict incorrect values like  2003-02-29T50:70:80.
      // Or not, that may be a breaking change.
      static DateTime parse(String formattedString) {
        var re = _parseFormat;
        Match? match = re.firstMatch(formattedString);
        if (match != null) {
      int parseIntOrZero(String? matched) {
        if (matched == null) return 0;
        return int.parse(matched);
      }
  
      // Parses fractional second digits of '.(\d+)' into the combined
      // microseconds. We only use the first 6 digits because of DateTime
      // precision of 999 milliseconds and 999 microseconds.
      int parseMilliAndMicroseconds(String? matched) {
        if (matched == null) return 0;
        int length = matched.length;
        assert(length &gt;= 1);
        int result = 0;
        for (int i = 0; i &lt; 6; i++) {
          result *= 10;
          if (i &lt; matched.length) {
            result += matched.codeUnitAt(i) ^ 0x30;
          }
        }
        return result;
      }
  
      int years = int.parse(match[1]!);
      int month = int.parse(match[2]!);
      int day = int.parse(match[3]!);
      int hour = parseIntOrZero(match[4]);
      int minute = parseIntOrZero(match[5]);
      int second = parseIntOrZero(match[6]);
      int milliAndMicroseconds = parseMilliAndMicroseconds(match[7]);
      int millisecond =
          milliAndMicroseconds ~/ Duration.microsecondsPerMillisecond;
      int microsecond = milliAndMicroseconds
          .remainder(Duration.microsecondsPerMillisecond) as int;
      bool isUtc = false;
      if (match[8] != null) {
        // timezone part
        isUtc = true;
        String? tzSign = match[9];
        if (tzSign != null) {
          // timezone other than 'Z' and 'z'.
          int sign = (tzSign == '-') ? -1 : 1;
          int hourDifference = int.parse(match[10]!);
          int minuteDifference = parseIntOrZero(match[11]);
          minuteDifference += 60 * hourDifference;
          minute -= sign * minuteDifference;
        }
      }
      int? value = _brokenDownDateToValue(years, month, day, hour, minute,
          second, millisecond, microsecond, isUtc);
      if (value == null) {
        throw FormatException(&quot;Time out of range&quot;, formattedString);
      }
      return DateTime._withValue(value, isUtc: isUtc);
    } else {
      throw FormatException(&quot;Invalid date format&quot;, formattedString);
    }
  }
  </code></pre>