From 0f4d5d37c8e0bfb2b77b95bcd71debba61cd5b0d Mon Sep 17 00:00:00 2001 From: YeungKC <11473691+YeungKC@users.noreply.github.com> Date: Sun, 18 Oct 2026 12:46:53 +0900 Subject: [PATCH] data_detector: fix TypeError in TextCheckingResult.date and duration 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. --- packages/data_detector/lib/data_detector.dart | 4 ++-- packages/data_detector/test/data_detector_test.dart | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/packages/data_detector/lib/data_detector.dart b/packages/data_detector/lib/data_detector.dart index 6c7909e6..34458c36 100644 --- a/packages/data_detector/lib/data_detector.dart +++ b/packages/data_detector/lib/data_detector.dart @@ -39,11 +39,11 @@ class TextCheckingResult { if (timestamp == null) { return null; } - return DateTime.fromMicrosecondsSinceEpoch((timestamp * 1e6) as int); + return DateTime.fromMicrosecondsSinceEpoch((timestamp * 1e6).round()); } Duration get duration => - Duration(microseconds: (_inner.duration * 1e6) as int); + Duration(microseconds: (_inner.duration * 1e6).round()); Uri? get url { final url = _inner.URL?.absoluteString; diff --git a/packages/data_detector/test/data_detector_test.dart b/packages/data_detector/test/data_detector_test.dart index ecd7ea02..454cc692 100644 --- a/packages/data_detector/test/data_detector_test.dart +++ b/packages/data_detector/test/data_detector_test.dart @@ -1,5 +1,6 @@ +import 'dart:io'; + import 'package:data_detector/data_detector.dart'; -import 'package:flutter/foundation.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { @@ -10,5 +11,13 @@ void main() { for (final result in results) { expect(result.range.textInside(str), "https://mixin.one"); } - }, skip: defaultTargetPlatform != TargetPlatform.macOS); + }, skip: !Platform.isMacOS); + + test("date", () { + final detector = DataDetector(NSTextCheckingType.NSTextCheckingTypeDate); + final results = detector.matchesInString("on 2024-01-02 10:00"); + expect(results, isNotEmpty); + expect(results.first.date, isNotNull); + expect(results.first.duration, Duration.zero); + }, skip: !Platform.isMacOS); }