Skip to content

data_detector: fix TypeError in TextCheckingResult.date and duration - #527

Merged
boyan01 merged 2 commits into
mainfrom
data_detector/fix-double-cast
Nov 9, 2026
Merged

boyan01 merged 2 commits into
mainfrom
data_detector/fix-double-cast

Conversation

@YeungKC

@YeungKC YeungKC commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Summary

TextCheckingResult.date and TextCheckingResult.duration cast a double (NSTimeInterval * 1e6) to int with as int. This cast always fails in Dart, so each call threw a TypeError.

-DateTime.fromMicrosecondsSinceEpoch((timestamp * 1e6) as int)
+DateTime.fromMicrosecondsSinceEpoch((timestamp * 1e6).round())
-Duration(microseconds: (_inner.duration * 1e6) as int)
+Duration(microseconds: (_inner.duration * 1e6).round())

The tests now skip with !Platform.isMacOS. Under flutter test, defaultTargetPlatform is android on all hosts, so the old condition skipped the tests everywhere.

Evidence

New test date: detect NSTextCheckingTypeDate in "on 2024-01-02 10:00", then read date and duration. Run with flutter test on macOS.

  • Before: fails with type 'double' is not a subtype of type 'int' in type cast
    After: +2: All tests passed! (the existing link test now runs too)

Merge Danger

Door: two-way

Blast Radius: small

Only the two getters change. Before, they always threw, so no caller can depend on the old result.

Both getters cast a double (NSTimeInterval * 1e6) to int with `as int`.
This cast always fails, so every call threw a TypeError. Round the
value to int instead.

The tests now skip with Platform.isMacOS. defaultTargetPlatform is
android under flutter test, so the old skip condition skipped them on
all hosts.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The narrow fix addresses both invalid casts with regression coverage; macOS tests were not independently rerun.

No open issues 🎉

What changed in this PR

Fixes invalid numeric casts in data_detector’s date and duration getters.

Changes:

  • Rounds microsecond values to integers instead of casting.
  • Enables tests on macOS using Platform.isMacOS.
  • Adds regression coverage for both getters.
File Description
packages/​data_detector/​test/​data_detector_test.dart Corrects platform checks and adds a date regression test.
packages/​data_detector/​lib/​data_detector.dart Replaces failing casts with .round().

Review effort: Balanced

@boyan01
boyan01 merged commit 512c173 into main Nov 9, 2026
7 checks passed
@boyan01
boyan01 deleted the data_detector/fix-double-cast branch November 9, 2026 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants