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.

<!-- Links -->
[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
This commit is contained in:
Alex Talebi 2025-07-23 01:41:01 +03:30 committed by GitHub
parent e12d263d3d
commit c45a67e4ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 2 deletions

View File

@ -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),

View File

@ -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')),
),
),
);
});
}