Skip to content

Commit 720bea0

Browse files
authored
Remove unnecessary null checks in flutter/widgets (#119028)
* dart fix --apply * manual fixes
1 parent 1906ce5 commit 720bea0

File tree

106 files changed

+372
-1374
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

106 files changed

+372
-1374
lines changed

packages/flutter/lib/src/widgets/actions.dart

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,7 @@ class ActionListener extends StatefulWidget {
431431
required this.listener,
432432
required this.action,
433433
required this.child,
434-
}) : assert(listener != null),
435-
assert(action != null),
436-
assert(child != null);
434+
});
437435

438436
/// The [ActionListenerCallback] callback to register with the [action].
439437
///
@@ -548,7 +546,7 @@ class CallbackAction<T extends Intent> extends Action<T> {
548546
///
549547
/// The `intentKey` and [onInvoke] parameters must not be null.
550548
/// The [onInvoke] parameter is required.
551-
CallbackAction({required this.onInvoke}) : assert(onInvoke != null);
549+
CallbackAction({required this.onInvoke});
552550

553551
/// The callback to be called when invoked.
554552
///
@@ -595,8 +593,6 @@ class ActionDispatcher with Diagnosticable {
595593
covariant Intent intent, [
596594
BuildContext? context,
597595
]) {
598-
assert(action != null);
599-
assert(intent != null);
600596
assert(action.isEnabled(intent), 'Action must be enabled when calling invokeAction');
601597
if (action is ContextAction) {
602598
context ??= primaryFocus?.context;
@@ -651,8 +647,7 @@ class Actions extends StatefulWidget {
651647
this.dispatcher,
652648
required this.actions,
653649
required this.child,
654-
}) : assert(actions != null),
655-
assert(child != null);
650+
});
656651

657652
/// The [ActionDispatcher] object that invokes actions.
658653
///
@@ -869,7 +864,6 @@ class Actions extends StatefulWidget {
869864
/// Will return a newly created [ActionDispatcher] if no ambient [Actions]
870865
/// widget is found.
871866
static ActionDispatcher of(BuildContext context) {
872-
assert(context != null);
873867
final _ActionsScope? marker = context.dependOnInheritedWidgetOfExactType<_ActionsScope>();
874868
return marker?.dispatcher ?? _findDispatcher(context);
875869
}
@@ -892,8 +886,6 @@ class Actions extends StatefulWidget {
892886
BuildContext context,
893887
T intent,
894888
) {
895-
assert(intent != null);
896-
assert(context != null);
897889
Object? returnValue;
898890

899891
final bool actionFound = _visitActionsAncestors(context, (InheritedElement element) {
@@ -945,8 +937,6 @@ class Actions extends StatefulWidget {
945937
BuildContext context,
946938
T intent,
947939
) {
948-
assert(intent != null);
949-
assert(context != null);
950940
Object? returnValue;
951941

952942
_visitActionsAncestors(context, (InheritedElement element) {
@@ -1041,8 +1031,7 @@ class _ActionsScope extends InheritedWidget {
10411031
required this.actions,
10421032
required this.rebuildKey,
10431033
required super.child,
1044-
}) : assert(child != null),
1045-
assert(actions != null);
1034+
});
10461035

10471036
final ActionDispatcher? dispatcher;
10481037
final Map<Type, Action<Intent>> actions;
@@ -1106,10 +1095,7 @@ class FocusableActionDetector extends StatefulWidget {
11061095
this.mouseCursor = MouseCursor.defer,
11071096
this.includeFocusSemantics = true,
11081097
required this.child,
1109-
}) : assert(enabled != null),
1110-
assert(autofocus != null),
1111-
assert(mouseCursor != null),
1112-
assert(child != null);
1098+
});
11131099

11141100
/// Is this widget enabled or not.
11151101
///
@@ -1530,7 +1516,7 @@ class PrioritizedIntents extends Intent {
15301516
/// of intents, the first available of which will be used.
15311517
const PrioritizedIntents({
15321518
required this.orderedIntents,
1533-
}) : assert(orderedIntents != null);
1519+
});
15341520

15351521
/// List of intents to be evaluated in order for execution. When an
15361522
/// [Action.isEnabled] returns true, that action will be invoked and
@@ -1566,8 +1552,6 @@ class PrioritizedAction extends Action<PrioritizedIntents> {
15661552

15671553
@override
15681554
void invoke(PrioritizedIntents intent) {
1569-
assert(_selectedAction != null);
1570-
assert(_selectedIntent != null);
15711555
_selectedAction.invoke(_selectedIntent);
15721556
}
15731557
}

packages/flutter/lib/src/widgets/animated_cross_fade.dart

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,7 @@ class AnimatedCrossFade extends StatefulWidget {
130130
this.reverseDuration,
131131
this.layoutBuilder = defaultLayoutBuilder,
132132
this.excludeBottomFocus = true,
133-
}) : assert(firstChild != null),
134-
assert(secondChild != null),
135-
assert(firstCurve != null),
136-
assert(secondCurve != null),
137-
assert(sizeCurve != null),
138-
assert(alignment != null),
139-
assert(crossFadeState != null),
140-
assert(duration != null),
141-
assert(layoutBuilder != null),
142-
assert(excludeBottomFocus != null);
133+
});
143134

144135
/// The child that is visible when [crossFadeState] is
145136
/// [CrossFadeState.showFirst]. It fades out when transitioning

packages/flutter/lib/src/widgets/animated_scroll_view.dart

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ class AnimatedList extends _AnimatedScrollView {
5252
super.shrinkWrap = false,
5353
super.padding,
5454
super.clipBehavior = Clip.hardEdge,
55-
}) : assert(itemBuilder != null),
56-
assert(initialItemCount != null && initialItemCount >= 0);
55+
}) : assert(initialItemCount >= 0);
5756

5857
/// The state from the closest instance of this class that encloses the given
5958
/// context.
@@ -74,7 +73,6 @@ class AnimatedList extends _AnimatedScrollView {
7473
/// * [maybeOf], a similar function that will return null if no
7574
/// [AnimatedList] ancestor is found.
7675
static AnimatedListState of(BuildContext context) {
77-
assert(context != null);
7876
final AnimatedListState? result = AnimatedList.maybeOf(context);
7977
assert(() {
8078
if (result == null) {
@@ -116,7 +114,6 @@ class AnimatedList extends _AnimatedScrollView {
116114
/// * [of], a similar function that will throw if no [AnimatedList] ancestor
117115
/// is found.
118116
static AnimatedListState? maybeOf(BuildContext context) {
119-
assert(context != null);
120117
return context.findAncestorStateOfType<AnimatedListState>();
121118
}
122119

@@ -217,8 +214,7 @@ class AnimatedGrid extends _AnimatedScrollView {
217214
super.physics,
218215
super.padding,
219216
super.clipBehavior = Clip.hardEdge,
220-
}) : assert(itemBuilder != null),
221-
assert(initialItemCount != null && initialItemCount >= 0);
217+
}) : assert(initialItemCount >= 0);
222218

223219
/// {@template flutter.widgets.AnimatedGrid.gridDelegate}
224220
/// A delegate that controls the layout of the children within the
@@ -252,7 +248,6 @@ class AnimatedGrid extends _AnimatedScrollView {
252248
/// * [maybeOf], a similar function that will return null if no
253249
/// [AnimatedGrid] ancestor is found.
254250
static AnimatedGridState of(BuildContext context) {
255-
assert(context != null);
256251
final AnimatedGridState? result = AnimatedGrid.maybeOf(context);
257252
assert(() {
258253
if (result == null) {
@@ -294,7 +289,6 @@ class AnimatedGrid extends _AnimatedScrollView {
294289
/// * [of], a similar function that will throw if no [AnimatedGrid] ancestor
295290
/// is found.
296291
static AnimatedGridState? maybeOf(BuildContext context) {
297-
assert(context != null);
298292
return context.findAncestorStateOfType<AnimatedGridState>();
299293
}
300294

@@ -373,8 +367,7 @@ abstract class _AnimatedScrollView extends StatefulWidget {
373367
this.shrinkWrap = false,
374368
this.padding,
375369
this.clipBehavior = Clip.hardEdge,
376-
}) : assert(itemBuilder != null),
377-
assert(initialItemCount != null && initialItemCount >= 0);
370+
}) : assert(initialItemCount >= 0);
378371

379372
/// {@template flutter.widgets.AnimatedScrollView.itemBuilder}
380373
/// Called, as needed, to build children widgets.
@@ -630,8 +623,7 @@ class SliverAnimatedList extends _SliverAnimatedMultiBoxAdaptor {
630623
required super.itemBuilder,
631624
super.findChildIndexCallback,
632625
super.initialItemCount = 0,
633-
}) : assert(itemBuilder != null),
634-
assert(initialItemCount != null && initialItemCount >= 0);
626+
}) : assert(initialItemCount >= 0);
635627

636628
@override
637629
SliverAnimatedListState createState() => SliverAnimatedListState();
@@ -655,7 +647,6 @@ class SliverAnimatedList extends _SliverAnimatedMultiBoxAdaptor {
655647
/// * [maybeOf], a similar function that will return null if no
656648
/// [SliverAnimatedList] ancestor is found.
657649
static SliverAnimatedListState of(BuildContext context) {
658-
assert(context != null);
659650
final SliverAnimatedListState? result = SliverAnimatedList.maybeOf(context);
660651
assert(() {
661652
if (result == null) {
@@ -695,7 +686,6 @@ class SliverAnimatedList extends _SliverAnimatedMultiBoxAdaptor {
695686
/// * [of], a similar function that will throw if no [SliverAnimatedList]
696687
/// ancestor is found.
697688
static SliverAnimatedListState? maybeOf(BuildContext context) {
698-
assert(context != null);
699689
return context.findAncestorStateOfType<SliverAnimatedListState>();
700690
}
701691
}
@@ -782,8 +772,7 @@ class SliverAnimatedGrid extends _SliverAnimatedMultiBoxAdaptor {
782772
required this.gridDelegate,
783773
super.findChildIndexCallback,
784774
super.initialItemCount = 0,
785-
}) : assert(itemBuilder != null),
786-
assert(initialItemCount != null && initialItemCount >= 0);
775+
}) : assert(initialItemCount >= 0);
787776

788777
@override
789778
SliverAnimatedGridState createState() => SliverAnimatedGridState();
@@ -807,7 +796,6 @@ class SliverAnimatedGrid extends _SliverAnimatedMultiBoxAdaptor {
807796
/// * [maybeOf], a similar function that will return null if no
808797
/// [SliverAnimatedGrid] ancestor is found.
809798
static SliverAnimatedGridState of(BuildContext context) {
810-
assert(context != null);
811799
final SliverAnimatedGridState? result = context.findAncestorStateOfType<SliverAnimatedGridState>();
812800
assert(() {
813801
if (result == null) {
@@ -844,7 +832,6 @@ class SliverAnimatedGrid extends _SliverAnimatedMultiBoxAdaptor {
844832
/// * [of], a similar function that will throw if no [SliverAnimatedGrid]
845833
/// ancestor is found.
846834
static SliverAnimatedGridState? maybeOf(BuildContext context) {
847-
assert(context != null);
848835
return context.findAncestorStateOfType<SliverAnimatedGridState>();
849836
}
850837
}
@@ -908,8 +895,7 @@ abstract class _SliverAnimatedMultiBoxAdaptor extends StatefulWidget {
908895
required this.itemBuilder,
909896
this.findChildIndexCallback,
910897
this.initialItemCount = 0,
911-
}) : assert(itemBuilder != null),
912-
assert(initialItemCount != null && initialItemCount >= 0);
898+
}) : assert(initialItemCount >= 0);
913899

914900
/// {@macro flutter.widgets.AnimatedScrollView.itemBuilder}
915901
final AnimatedItemBuilder itemBuilder;
@@ -1022,8 +1008,7 @@ abstract class _SliverAnimatedMultiBoxAdaptorState<T extends _SliverAnimatedMult
10221008
/// increases the length of the list of items by one and shifts
10231009
/// all items at or after [index] towards the end of the list of items.
10241010
void insertItem(int index, { Duration duration = _kDuration }) {
1025-
assert(index != null && index >= 0);
1026-
assert(duration != null);
1011+
assert(index >= 0);
10271012

10281013
final int itemIndex = _indexToItemIndex(index);
10291014
assert(itemIndex >= 0 && itemIndex <= _itemsCount);
@@ -1074,9 +1059,7 @@ abstract class _SliverAnimatedMultiBoxAdaptorState<T extends _SliverAnimatedMult
10741059
/// decreases the length of items by one and shifts
10751060
/// all items at or before [index] towards the beginning of the list of items.
10761061
void removeItem(int index, AnimatedRemovedItemBuilder builder, { Duration duration = _kDuration }) {
1077-
assert(index != null && index >= 0);
1078-
assert(builder != null);
1079-
assert(duration != null);
1062+
assert(index >= 0);
10801063

10811064
final int itemIndex = _indexToItemIndex(index);
10821065
assert(itemIndex >= 0 && itemIndex < _itemsCount);

packages/flutter/lib/src/widgets/animated_size.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class AnimatedSize extends StatefulWidget {
3838
)
3939
TickerProvider? vsync,
4040
this.clipBehavior = Clip.hardEdge,
41-
}) : assert(clipBehavior != null);
41+
});
4242

4343
/// The widget below this widget in the tree.
4444
///
@@ -114,7 +114,7 @@ class _AnimatedSize extends SingleChildRenderObjectWidget {
114114
this.reverseDuration,
115115
required this.vsync,
116116
this.clipBehavior = Clip.hardEdge,
117-
}) : assert(clipBehavior != null);
117+
});
118118

119119
final AlignmentGeometry alignment;
120120
final Curve curve;

packages/flutter/lib/src/widgets/animated_switcher.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ class _ChildEntry {
1919
required this.animation,
2020
required this.transition,
2121
required this.widgetChild,
22-
}) : assert(animation != null),
23-
assert(transition != null),
24-
assert(controller != null);
22+
});
2523

2624
// The animation controller for the child's transition.
2725
final AnimationController controller;
@@ -117,11 +115,7 @@ class AnimatedSwitcher extends StatefulWidget {
117115
this.switchOutCurve = Curves.linear,
118116
this.transitionBuilder = AnimatedSwitcher.defaultTransitionBuilder,
119117
this.layoutBuilder = AnimatedSwitcher.defaultLayoutBuilder,
120-
}) : assert(duration != null),
121-
assert(switchInCurve != null),
122-
assert(switchOutCurve != null),
123-
assert(transitionBuilder != null),
124-
assert(layoutBuilder != null);
118+
});
125119

126120
/// The current child widget to display. If there was a previous child, then
127121
/// that child will be faded out using the [switchOutCurve], while the new

packages/flutter/lib/src/widgets/annotated_region.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ class AnnotatedRegion<T extends Object> extends SingleChildRenderObjectWidget {
2424
required Widget super.child,
2525
required this.value,
2626
this.sized = true,
27-
}) : assert(value != null),
28-
assert(child != null);
27+
});
2928

3029
/// A value which can be retrieved using [Layer.find].
3130
final T value;

packages/flutter/lib/src/widgets/app.dart

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ class WidgetsApp extends StatefulWidget {
344344
this.actions,
345345
this.restorationScopeId,
346346
this.useInheritedMediaQuery = false,
347-
}) : assert(navigatorObservers != null),
348-
assert(routes != null),
349-
assert(
347+
}) : assert(
350348
home == null ||
351349
onGenerateInitialRoutes == null,
352350
'If onGenerateInitialRoutes is specified, the home argument will be '
@@ -398,15 +396,7 @@ class WidgetsApp extends StatefulWidget {
398396
'pageRouteBuilder must be specified so that the default handler '
399397
'will know what kind of PageRoute transition to build.',
400398
),
401-
assert(title != null),
402-
assert(color != null),
403-
assert(supportedLocales != null && supportedLocales.isNotEmpty),
404-
assert(showPerformanceOverlay != null),
405-
assert(checkerboardRasterCacheImages != null),
406-
assert(checkerboardOffscreenLayers != null),
407-
assert(showSemanticsDebugger != null),
408-
assert(debugShowCheckedModeBanner != null),
409-
assert(debugShowWidgetInspector != null),
399+
assert(supportedLocales.isNotEmpty),
410400
routeInformationProvider = null,
411401
routeInformationParser = null,
412402
routerDelegate = null,
@@ -463,15 +453,7 @@ class WidgetsApp extends StatefulWidget {
463453
);
464454
return true;
465455
}()),
466-
assert(title != null),
467-
assert(color != null),
468-
assert(supportedLocales != null && supportedLocales.isNotEmpty),
469-
assert(showPerformanceOverlay != null),
470-
assert(checkerboardRasterCacheImages != null),
471-
assert(checkerboardOffscreenLayers != null),
472-
assert(showSemanticsDebugger != null),
473-
assert(debugShowCheckedModeBanner != null),
474-
assert(debugShowWidgetInspector != null),
456+
assert(supportedLocales.isNotEmpty),
475457
navigatorObservers = null,
476458
navigatorKey = null,
477459
onGenerateRoute = null,
@@ -1432,7 +1414,6 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
14321414
settings,
14331415
pageContentBuilder,
14341416
);
1435-
assert(route != null, 'The pageRouteBuilder for WidgetsApp must return a valid non-null Route.');
14361417
return route;
14371418
}
14381419
if (widget.onGenerateRoute != null) {
@@ -1722,7 +1703,6 @@ class _WidgetsAppState extends State<WidgetsApp> with WidgetsBindingObserver {
17221703
// parameter.
17231704
builder: (BuildContext context) {
17241705
final String title = widget.onGenerateTitle!(context);
1725-
assert(title != null, 'onGenerateTitle must return a non-null String');
17261706
return Title(
17271707
title: title,
17281708
color: widget.color.withOpacity(1.0),

0 commit comments

Comments
 (0)