Unverified Commit aad941e3 authored by Albertus Angga Raharja's avatar Albertus Angga Raharja Committed by GitHub

Avoid using FlutterError.fromParts when possible (#43696)

This PR is a follow up of https://github.com/flutter/flutter/pull/42640
Some changes of that PR includes redundant changes using FlutterError.fromParts constructor even though it's not necessary.

Some minor changes are:

- Remove one unnecessary todo
- Fix indent consistencies
parent d030296a
...@@ -439,14 +439,12 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do ...@@ -439,14 +439,12 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do
final double transformedValue = activeCurve.transform(t); final double transformedValue = activeCurve.transform(t);
final double roundedTransformedValue = transformedValue.round().toDouble(); final double roundedTransformedValue = transformedValue.round().toDouble();
if (roundedTransformedValue != t) { if (roundedTransformedValue != t) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('Invalid curve endpoint at $t.'), 'Invalid curve endpoint at $t.\n'
ErrorDescription( 'Curves must map 0.0 to near zero and 1.0 to near one but '
'Curves must map 0.0 to near zero and 1.0 to near one but ' '${activeCurve.runtimeType} mapped $t to $transformedValue, which '
'${activeCurve.runtimeType} mapped $t to $transformedValue, which ' 'is near $roundedTransformedValue.'
'is near $roundedTransformedValue.' );
)
]);
} }
return true; return true;
}()); }());
......
...@@ -192,33 +192,29 @@ class _CupertinoTabViewState extends State<CupertinoTabView> { ...@@ -192,33 +192,29 @@ class _CupertinoTabViewState extends State<CupertinoTabView> {
Route<dynamic> _onUnknownRoute(RouteSettings settings) { Route<dynamic> _onUnknownRoute(RouteSettings settings) {
assert(() { assert(() {
if (widget.onUnknownRoute == null) { if (widget.onUnknownRoute == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('Could not find a generator for route $settings in the $runtimeType.'), 'Could not find a generator for route $settings in the $runtimeType.\n'
ErrorDescription( 'Generators for routes are searched for in the following order:\n'
'Generators for routes are searched for in the following order:\n' ' 1. For the "/" route, the "builder" property, if non-null, is used.\n'
' 1. For the "/" route, the "builder" property, if non-null, is used.\n' ' 2. Otherwise, the "routes" table is used, if it has an entry for '
' 2. Otherwise, the "routes" table is used, if it has an entry for ' 'the route.\n'
'the route.\n' ' 3. Otherwise, onGenerateRoute is called. It should return a '
' 3. Otherwise, onGenerateRoute is called. It should return a ' 'non-null value for any valid route not handled by "builder" and "routes".\n'
'non-null value for any valid route not handled by "builder" and "routes".\n' ' 4. Finally if all else fails onUnknownRoute is called.\n'
' 4. Finally if all else fails onUnknownRoute is called.\n' 'Unfortunately, onUnknownRoute was not set.'
'Unfortunately, onUnknownRoute was not set.' );
)
]);
} }
return true; return true;
}()); }());
final Route<dynamic> result = widget.onUnknownRoute(settings); final Route<dynamic> result = widget.onUnknownRoute(settings);
assert(() { assert(() {
if (result == null) { if (result == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The onUnknownRoute callback returned null.'), 'The onUnknownRoute callback returned null.\n'
ErrorDescription( 'When the $runtimeType requested the route $settings from its '
'When the $runtimeType requested the route $settings from its ' 'onUnknownRoute callback, the callback returned null. Such callbacks '
'onUnknownRoute callback, the callback returned null. Such callbacks ' 'must never return null.'
'must never return null.' );
)
]);
} }
return true; return true;
}()); }());
......
...@@ -102,10 +102,10 @@ class ChangeNotifier implements Listenable { ...@@ -102,10 +102,10 @@ class ChangeNotifier implements Listenable {
bool _debugAssertNotDisposed() { bool _debugAssertNotDisposed() {
assert(() { assert(() {
if (_listeners == null) { if (_listeners == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('A $runtimeType was used after being disposed.'), 'A $runtimeType was used after being disposed.\n'
ErrorDescription('Once you have called dispose() on a $runtimeType, it can no longer be used.') 'Once you have called dispose() on a $runtimeType, it can no longer be used.'
]); );
} }
return true; return true;
}()); }());
......
...@@ -82,10 +82,10 @@ class MaterialPageRoute<T> extends PageRoute<T> { ...@@ -82,10 +82,10 @@ class MaterialPageRoute<T> extends PageRoute<T> {
final Widget result = builder(context); final Widget result = builder(context);
assert(() { assert(() {
if (result == null) { if (result == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The builder for route "${settings.name}" returned null.'), 'The builder for route "${settings.name}" returned null.\n'
ErrorDescription('Route builders must never return null.') 'Route builders must never return null.'
]); );
} }
return true; return true;
}()); }());
......
...@@ -142,16 +142,16 @@ abstract class MultiChildLayoutDelegate { ...@@ -142,16 +142,16 @@ abstract class MultiChildLayoutDelegate {
final RenderBox child = _idToChild[childId]; final RenderBox child = _idToChild[childId];
assert(() { assert(() {
if (child == null) { if (child == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The $this custom multichild layout delegate tried to lay out a non-existent child.'), 'The $this custom multichild layout delegate tried to lay out a non-existent child.\n'
ErrorDescription('There is no child with the id "$childId".') 'There is no child with the id "$childId".'
]); );
} }
if (!_debugChildrenNeedingLayout.remove(child)) { if (!_debugChildrenNeedingLayout.remove(child)) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The $this custom multichild layout delegate tried to lay out the child with id "$childId" more than once.'), 'The $this custom multichild layout delegate tried to lay out the child with id "$childId" more than once.\n'
ErrorDescription('Each child must be laid out exactly once.') 'Each child must be laid out exactly once.'
]); );
} }
try { try {
assert(constraints.debugAssertIsValid(isAppliedConstraint: true)); assert(constraints.debugAssertIsValid(isAppliedConstraint: true));
...@@ -182,15 +182,15 @@ abstract class MultiChildLayoutDelegate { ...@@ -182,15 +182,15 @@ abstract class MultiChildLayoutDelegate {
final RenderBox child = _idToChild[childId]; final RenderBox child = _idToChild[childId];
assert(() { assert(() {
if (child == null) { if (child == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The $this custom multichild layout delegate tried to position out a non-existent child:'), 'The $this custom multichild layout delegate tried to position out a non-existent child:\n'
ErrorDescription('There is no child with the id "$childId".') 'There is no child with the id "$childId".'
]); );
} }
if (offset == null) { if (offset == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The $this custom multichild layout delegate provided a null position for the child with id "$childId".') 'The $this custom multichild layout delegate provided a null position for the child with id "$childId".'
]); );
} }
return true; return true;
}()); }());
......
...@@ -318,13 +318,11 @@ class RenderFlow extends RenderBox ...@@ -318,13 +318,11 @@ class RenderFlow extends RenderBox
final FlowParentData childParentData = child.parentData as FlowParentData; final FlowParentData childParentData = child.parentData as FlowParentData;
assert(() { assert(() {
if (childParentData._transform != null) { if (childParentData._transform != null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('Cannot call paintChild twice for the same child.'), 'Cannot call paintChild twice for the same child.\n'
ErrorDescription( 'The flow delegate of type ${_delegate.runtimeType} attempted to '
'The flow delegate of type ${_delegate.runtimeType} attempted to ' 'paint child $i multiple times, which is not permitted.'
'paint child $i multiple times, which is not permitted.' );
)
]);
} }
return true; return true;
}()); }());
......
...@@ -461,15 +461,13 @@ class RenderAspectRatio extends RenderProxyBox { ...@@ -461,15 +461,13 @@ class RenderAspectRatio extends RenderProxyBox {
assert(constraints.debugAssertIsValid()); assert(constraints.debugAssertIsValid());
assert(() { assert(() {
if (!constraints.hasBoundedWidth && !constraints.hasBoundedHeight) { if (!constraints.hasBoundedWidth && !constraints.hasBoundedHeight) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('$runtimeType has unbounded constraints.'), '$runtimeType has unbounded constraints.\n'
ErrorDescription( 'This $runtimeType was given an aspect ratio of $aspectRatio but was given '
'This $runtimeType was given an aspect ratio of $aspectRatio but was given ' 'both unbounded width and unbounded height constraints. Because both '
'both unbounded width and unbounded height constraints. Because both ' 'constraints were unbounded, this render object doesn\'t know how much '
"constraints were unbounded, this render object doesn't know how much " 'size to consume.'
'size to consume.' );
)
]);
} }
return true; return true;
}()); }());
......
...@@ -1018,14 +1018,12 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver { ...@@ -1018,14 +1018,12 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
final Route<dynamic> result = widget.onUnknownRoute(settings); final Route<dynamic> result = widget.onUnknownRoute(settings);
assert(() { assert(() {
if (result == null) { if (result == null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('The onUnknownRoute callback returned null.'), 'The onUnknownRoute callback returned null.\n'
ErrorDescription( 'When the $runtimeType requested the route $settings from its '
'When the $runtimeType requested the route $settings from its ' 'onUnknownRoute callback, the callback returned null. Such callbacks '
'onUnknownRoute callback, the callback returned null. Such callbacks ' 'must never return null.'
'must never return null.' );
)
]);
} }
return true; return true;
}()); }());
......
...@@ -136,13 +136,11 @@ bool debugChildrenHaveDuplicateKeys(Widget parent, Iterable<Widget> children) { ...@@ -136,13 +136,11 @@ bool debugChildrenHaveDuplicateKeys(Widget parent, Iterable<Widget> children) {
assert(() { assert(() {
final Key nonUniqueKey = _firstNonUniqueKey(children); final Key nonUniqueKey = _firstNonUniqueKey(children);
if (nonUniqueKey != null) { if (nonUniqueKey != null) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('Duplicate keys found.'), 'Duplicate keys found.\n'
ErrorDescription( 'If multiple keyed nodes exist as children of another node, they must have unique keys.\n'
'If multiple keyed nodes exist as children of another node, they must have unique keys.\n' '$parent has multiple children with key $nonUniqueKey.'
'$parent has multiple children with key $nonUniqueKey.' );
),
]);
} }
return true; return true;
}()); }());
...@@ -165,9 +163,7 @@ bool debugItemsHaveDuplicateKeys(Iterable<Widget> items) { ...@@ -165,9 +163,7 @@ bool debugItemsHaveDuplicateKeys(Iterable<Widget> items) {
assert(() { assert(() {
final Key nonUniqueKey = _firstNonUniqueKey(items); final Key nonUniqueKey = _firstNonUniqueKey(items);
if (nonUniqueKey != null) if (nonUniqueKey != null)
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError('Duplicate key found: $nonUniqueKey.');
ErrorSummary('Duplicate key found: $nonUniqueKey.'),
]);
return true; return true;
}()); }());
return false; return false;
...@@ -315,7 +311,7 @@ bool debugAssertAllWidgetVarsUnset(String reason) { ...@@ -315,7 +311,7 @@ bool debugAssertAllWidgetVarsUnset(String reason) {
debugPrintGlobalKeyedWidgetLifecycle || debugPrintGlobalKeyedWidgetLifecycle ||
debugProfileBuildsEnabled || debugProfileBuildsEnabled ||
debugHighlightDeprecatedWidgets) { debugHighlightDeprecatedWidgets) {
throw FlutterError.fromParts(<DiagnosticsNode>[ErrorSummary(reason)]); throw FlutterError(reason);
} }
return true; return true;
}()); }());
......
...@@ -190,7 +190,7 @@ class TextEditingController extends ValueNotifier<TextEditingValue> { ...@@ -190,7 +190,7 @@ class TextEditingController extends ValueNotifier<TextEditingValue> {
/// change the controller's [value]. /// change the controller's [value].
set selection(TextSelection newSelection) { set selection(TextSelection newSelection) {
if (newSelection.start > text.length || newSelection.end > text.length) if (newSelection.start > text.length || newSelection.end > text.length)
throw FlutterError.fromParts(<DiagnosticsNode>[ErrorSummary('invalid text selection: $newSelection')]); throw FlutterError('invalid text selection: $newSelection');
value = value.copyWith(selection: newSelection, composing: TextRange.empty); value = value.copyWith(selection: newSelection, composing: TextRange.empty);
} }
......
...@@ -242,13 +242,11 @@ class GestureDetector extends StatelessWidget { ...@@ -242,13 +242,11 @@ class GestureDetector extends StatelessWidget {
} }
final String recognizer = havePan ? 'pan' : 'scale'; final String recognizer = havePan ? 'pan' : 'scale';
if (haveVerticalDrag && haveHorizontalDrag) { if (haveVerticalDrag && haveHorizontalDrag) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('Incorrect GestureDetector arguments.'), 'Incorrect GestureDetector arguments.\n'
ErrorDescription( 'Simultaneously having a vertical drag gesture recognizer, a horizontal drag gesture recognizer, and a $recognizer gesture recognizer '
'Simultaneously having a vertical drag gesture recognizer, a horizontal drag gesture recognizer, and a $recognizer gesture recognizer ' 'will result in the $recognizer gesture recognizer being ignored, since the other two will catch all drags.'
'will result in the $recognizer gesture recognizer being ignored, since the other two will catch all drags.' );
)
]);
} }
} }
return true; return true;
......
...@@ -229,14 +229,12 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics { ...@@ -229,14 +229,12 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
assert(() { assert(() {
final double delta = newPixels - pixels; final double delta = newPixels - pixels;
if (overscroll.abs() > delta.abs()) { if (overscroll.abs() > delta.abs()) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('$runtimeType.applyBoundaryConditions returned invalid overscroll value.'), '$runtimeType.applyBoundaryConditions returned invalid overscroll value.\n'
ErrorDescription( 'setPixels() was called to change the scroll offset from $pixels to $newPixels.\n'
'setPixels() was called to change the scroll offset from $pixels to $newPixels.\n' 'That is a delta of $delta units.\n'
'That is a delta of $delta units.\n' '$runtimeType.applyBoundaryConditions reported an overscroll of $overscroll units.'
'$runtimeType.applyBoundaryConditions reported an overscroll of $overscroll units.' );
)
]);
} }
return true; return true;
}()); }());
...@@ -402,18 +400,16 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics { ...@@ -402,18 +400,16 @@ abstract class ScrollPosition extends ViewportOffset with ScrollMetrics {
assert(() { assert(() {
final double delta = value - pixels; final double delta = value - pixels;
if (result.abs() > delta.abs()) { if (result.abs() > delta.abs()) {
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError(
ErrorSummary('${physics.runtimeType}.applyBoundaryConditions returned invalid overscroll value.'), '${physics.runtimeType}.applyBoundaryConditions returned invalid overscroll value.\n'
ErrorDescription( 'The method was called to consider a change from $pixels to $value, which is a '
'The method was called to consider a change from $pixels to $value, which is a ' 'delta of ${delta.toStringAsFixed(1)} units. However, it returned an overscroll of '
'delta of ${delta.toStringAsFixed(1)} units. However, it returned an overscroll of ' '${result.toStringAsFixed(1)} units, which has a greater magnitude than the delta. '
'${result.toStringAsFixed(1)} units, which has a greater magnitude than the delta. ' 'The applyBoundaryConditions method is only supposed to reduce the possible range '
'The applyBoundaryConditions method is only supposed to reduce the possible range ' 'of movement, not increase it.\n'
'of movement, not increase it.\n' 'The scroll extents are $minScrollExtent .. $maxScrollExtent, and the '
'The scroll extents are $minScrollExtent .. $maxScrollExtent, and the ' 'viewport dimension is $viewportDimension.'
'viewport dimension is $viewportDimension.' );
)
]);
} }
return true; return true;
}()); }());
......
...@@ -780,8 +780,6 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker ...@@ -780,8 +780,6 @@ class WidgetTester extends WidgetController implements HitTestDispatcher, Ticker
void _verifySemanticsHandlesWereDisposed() { void _verifySemanticsHandlesWereDisposed() {
assert(_lastRecordedSemanticsHandles != null); assert(_lastRecordedSemanticsHandles != null);
if (binding.pipelineOwner.debugOutstandingSemanticsHandles > _lastRecordedSemanticsHandles) { if (binding.pipelineOwner.debugOutstandingSemanticsHandles > _lastRecordedSemanticsHandles) {
// TODO(jacobr): The hint for this one causes a change in line breaks but
// I think it is for the best.
throw FlutterError.fromParts(<DiagnosticsNode>[ throw FlutterError.fromParts(<DiagnosticsNode>[
ErrorSummary('A SemanticsHandle was active at the end of the test.'), ErrorSummary('A SemanticsHandle was active at the end of the test.'),
ErrorDescription( ErrorDescription(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment