From c45a67e4acadaed89d605faa71a26d8ee261c671 Mon Sep 17 00:00:00 2001 From: Alex Talebi <31685655+SalehTZ@users.noreply.github.com> Date: Wed, 23 Jul 2025 01:41:01 +0330 Subject: [PATCH] Improve assertion message in `AlignmentDirectional.resolve` (#172096) Replaces the generic assertion in `AlignmentDirectional.resolve` and `_MixedAlignment.resolve` with `debugCheckCanResolveTextDirection`. This change provides a more helpful and descriptive error message when the `TextDirection` is null, improving the developer experience by clearly stating why the resolution failed and how to fix it. Fixes: #171813, Fixes: #171814, also fixes: #171812 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --- .../flutter/lib/src/painting/alignment.dart | 5 +-- .../flutter/test/painting/alignment_test.dart | 32 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/painting/alignment.dart b/packages/flutter/lib/src/painting/alignment.dart index 6f3903406e6..2a1365b6721 100644 --- a/packages/flutter/lib/src/painting/alignment.dart +++ b/packages/flutter/lib/src/painting/alignment.dart @@ -11,6 +11,7 @@ import 'dart:ui' as ui show lerpDouble; import 'package:flutter/foundation.dart'; import 'basic_types.dart'; +import 'debug.dart'; /// Base class for [Alignment] that allows for text-direction aware /// resolution. @@ -584,7 +585,7 @@ class AlignmentDirectional extends AlignmentGeometry { @override Alignment resolve(TextDirection? direction) { - assert(direction != null, 'Cannot resolve $runtimeType without a TextDirection.'); + assert(debugCheckCanResolveTextDirection(direction, '$AlignmentDirectional')); return switch (direction!) { TextDirection.rtl => Alignment(-start, y), TextDirection.ltr => Alignment(start, y), @@ -653,7 +654,7 @@ class _MixedAlignment extends AlignmentGeometry { @override Alignment resolve(TextDirection? direction) { - assert(direction != null, 'Cannot resolve $runtimeType without a TextDirection.'); + assert(debugCheckCanResolveTextDirection(direction, '$_MixedAlignment')); return switch (direction!) { TextDirection.rtl => Alignment(_x - _start, _y), TextDirection.ltr => Alignment(_x + _start, _y), diff --git a/packages/flutter/test/painting/alignment_test.dart b/packages/flutter/test/painting/alignment_test.dart index 815723b1934..e8c63f6fd3b 100644 --- a/packages/flutter/test/painting/alignment_test.dart +++ b/packages/flutter/test/painting/alignment_test.dart @@ -4,6 +4,7 @@ import 'dart:math' as math; +import 'package:flutter/foundation.dart'; import 'package:flutter/painting.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -350,4 +351,35 @@ void main() { expect(AlignmentGeometry.bottomCenter, Alignment.bottomCenter); expect(AlignmentGeometry.bottomRight, Alignment.bottomRight); }); + + test('AlignmentDirectional.resolve with null TextDirection asserts', () { + const AlignmentDirectional alignmentDirectional = AlignmentDirectional(1.0, 2.0); + + expect( + () => alignmentDirectional.resolve(null), + throwsA( + isFlutterError.having( + (FlutterError e) => e.message, + 'message', + allOf(contains('No TextDirection found.'), contains('without a Directionality ancestor')), + ), + ), + ); + }); + + test('AlignmentDirectional.resolve with null TextDirection asserts', () { + const Alignment a = Alignment(5.0, 6.0); + const AlignmentDirectional b = AlignmentDirectional(15.0, 16.0); + + expect( + () => a.add(b).resolve(null), + throwsA( + isFlutterError.having( + (FlutterError e) => e.message, + 'message', + allOf(contains('No TextDirection found.'), contains('without a Directionality ancestor')), + ), + ), + ); + }); }