Skip to content

[convert] FixedDateTimeFormatter.tryDecode throws RangeError when the input is shorter than the pattern #1039

Open

Description

@uveshm003

FixedDateTimeFormatter.tryDecode is documented to return null when the input doesn't match the pattern, but an input shorter than the pattern throws a RangeError. decode also throws RangeError here, not the documented FormatException.

import 'package:convert/convert.dart';

void main() {
  final f = FixedDateTimeFormatter('YYYYMMDD');
  print(f.tryDecode('202401'));
}

Actual:

RangeError (length): Invalid value: Not in inclusive range 0..5: 6

Expected: null from tryDecode, and a FormatException from decode.

Cause: tryParse (pkgs/convert/lib/src/fixed_datetime_formatter.dart:325) loops to the block's end and calls codeUnitAt(i) without checking formattedDateTime.length.

Suggested fix: in tryParse, return null when end > formattedDateTime.length. The error message in _extractNumberFromString (line 312) also needs to clamp its substring end, or decode will still throw RangeError.

Activity

  1. FMorschel commented on Nov 3, 2026

    @FMorschel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions