mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Improve the error message for non-normalized constraints (#127906)
We probably added RenderBox.layout after this error was created and of course we weren't writing tests back then...
This commit is contained in:
parent
34f39a208e
commit
d64332a0aa
@ -2290,10 +2290,11 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
|
||||
informationCollector: () {
|
||||
final List<String> stack = StackTrace.current.toString().split('\n');
|
||||
int? targetFrame;
|
||||
final Pattern layoutFramePattern = RegExp(r'^#[0-9]+ +RenderObject.layout \(');
|
||||
final Pattern layoutFramePattern = RegExp(r'^#[0-9]+ +Render(?:Object|Box).layout \(');
|
||||
for (int i = 0; i < stack.length; i += 1) {
|
||||
if (layoutFramePattern.matchAsPrefix(stack[i]) != null) {
|
||||
targetFrame = i + 1;
|
||||
} else if (targetFrame != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -2301,7 +2302,6 @@ abstract class RenderObject extends AbstractNode with DiagnosticableTreeMixin im
|
||||
final Pattern targetFramePattern = RegExp(r'^#[0-9]+ +(.+)$');
|
||||
final Match? targetFrameMatch = targetFramePattern.matchAsPrefix(stack[targetFrame]);
|
||||
final String? problemFunction = (targetFrameMatch != null && targetFrameMatch.groupCount > 0) ? targetFrameMatch.group(1) : stack[targetFrame].trim();
|
||||
// TODO(jacobr): this case is similar to displaying a single stack frame.
|
||||
return <DiagnosticsNode>[
|
||||
ErrorDescription(
|
||||
"These invalid constraints were provided to $runtimeType's layout() "
|
||||
|
||||
@ -0,0 +1,43 @@
|
||||
// Copyright 2014 The Flutter Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// THIS TEST IS SENSITIVE TO LINE NUMBERS AT THE TOP OF THIS FILE
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter/widgets.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
class RenderFoo extends RenderShiftedBox {
|
||||
RenderFoo({ RenderBox? child }) : super(child);
|
||||
|
||||
@override
|
||||
void performLayout() {
|
||||
child?.layout(const BoxConstraints( // THIS MUST BE LINE 17
|
||||
minWidth: 100.0, maxWidth: 50.0,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
class Foo extends SingleChildRenderObjectWidget {
|
||||
const Foo({ super.key, super.child });
|
||||
|
||||
@override
|
||||
RenderFoo createRenderObject(BuildContext context) {
|
||||
return RenderFoo();
|
||||
}
|
||||
}
|
||||
|
||||
// END OF SENSITIVE SECTION
|
||||
|
||||
void main() {
|
||||
testWidgets('Stack parsing in non-normalized constraints error', (WidgetTester tester) async {
|
||||
await tester.pumpWidget(const Foo(child: Placeholder()), Duration.zero, EnginePhase.layout);
|
||||
final Object? exception = tester.takeException();
|
||||
final String text = exception.toString();
|
||||
expect(text, contains('BoxConstraints has non-normalized width constraints.'));
|
||||
expect(text, contains('which probably computed the invalid constraints in question:\n RenderFoo.performLayout ('));
|
||||
expect(text, contains('non_normalized_constraints_test.dart:17:12'));
|
||||
}, skip: kIsWeb); // [intended] stack traces on web are insufficiently predictable
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user