Skip to content

Commit 927af38

Browse files
committed
Null Safety Migration #2
*Make ResponsiveWrapperData values nonnull.
1 parent e413583 commit 927af38

File tree

2 files changed

+48
-49
lines changed

2 files changed

+48
-49
lines changed

lib/responsive_value.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ class ResponsiveValue<T> {
3939
}
4040

4141
List<Condition> conditions = valueWhen;
42-
List<ResponsiveBreakpointSegment?>? segments =
43-
ResponsiveWrapper.of(context)!.breakpointSegments;
42+
List<ResponsiveBreakpointSegment>? segments =
43+
ResponsiveWrapper.of(context).breakpointSegments;
4444
conditions = conditions.map((e) {
4545
if (e.breakpoint == null) {
4646
return e.copyWith(
47-
breakpoint: segments!
47+
breakpoint: segments
4848
.firstWhere(
49-
(element) => element!.responsiveBreakpoint.name == e.name,
49+
(element) => element.responsiveBreakpoint.name == e.name,
5050
orElse: () =>
51-
throw ('No breakpoint named `${e.name}` found.'))!
51+
throw ('No breakpoint named `${e.name}` found.'))
5252
.responsiveBreakpoint
5353
.breakpoint
5454
.toInt());
@@ -83,7 +83,7 @@ class ResponsiveValue<T> {
8383
BuildContext context, List<Condition> conditions) {
8484
Condition? equalsCondition = conditions.firstWhereOrNull((element) {
8585
if (element.condition == Conditional.EQUALS) {
86-
return ResponsiveWrapper.of(context)!.activeBreakpoint?.name ==
86+
return ResponsiveWrapper.of(context).activeBreakpoint.name ==
8787
element.name;
8888
}
8989

@@ -96,7 +96,7 @@ class ResponsiveValue<T> {
9696
Condition? smallerThanCondition = conditions.firstWhereOrNull((element) {
9797
if (element.condition == Conditional.SMALLER_THAN) {
9898
if (element.name != null) {
99-
return ResponsiveWrapper.of(context)!.isSmallerThan(element.name!);
99+
return ResponsiveWrapper.of(context).isSmallerThan(element.name!);
100100
}
101101

102102
return MediaQuery.of(context).size.width < element.breakpoint!;
@@ -111,7 +111,7 @@ class ResponsiveValue<T> {
111111
conditions.reversed.firstWhereOrNull((element) {
112112
if (element.condition == Conditional.LARGER_THAN) {
113113
if (element.name != null) {
114-
return ResponsiveWrapper.of(context)!.isLargerThan(element.name);
114+
return ResponsiveWrapper.of(context).isLargerThan(element.name);
115115
}
116116

117117
return MediaQuery.of(context).size.width >= element.breakpoint!;
@@ -237,8 +237,8 @@ class ResponsiveVisibility extends StatelessWidget {
237237
bool? visibleValue = visible;
238238

239239
// Combine Conditions.
240-
conditions.addAll(visibleWhen?.map((e) => e.copyWith(value: true)) ?? []);
241-
conditions.addAll(hiddenWhen?.map((e) => e.copyWith(value: false)) ?? []);
240+
conditions.addAll(visibleWhen.map((e) => e.copyWith(value: true)));
241+
conditions.addAll(hiddenWhen.map((e) => e.copyWith(value: false)));
242242
// Get visible value from active condition.
243243
visibleValue = ResponsiveValue(context,
244244
defaultValue: visibleValue, valueWhen: conditions)

lib/responsive_wrapper.dart

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -561,34 +561,34 @@ const String DESKTOP = 'DESKTOP';
561561
/// such as [ResponsiveWrapperData.scaledWidth].
562562
@immutable
563563
class ResponsiveWrapperData {
564-
final double? screenWidth;
565-
final double? screenHeight;
566-
final double? scaledWidth;
567-
final double? scaledHeight;
568-
final List<ResponsiveBreakpoint>? breakpoints;
569-
final List<ResponsiveBreakpointSegment?>? breakpointSegments;
570-
final ResponsiveBreakpoint? activeBreakpoint;
571-
final bool? isMobile;
572-
final bool? isPhone;
573-
final bool? isTablet;
574-
final bool? isDesktop;
564+
final double screenWidth;
565+
final double screenHeight;
566+
final double scaledWidth;
567+
final double scaledHeight;
568+
final List<ResponsiveBreakpoint> breakpoints;
569+
final List<ResponsiveBreakpointSegment> breakpointSegments;
570+
final ResponsiveBreakpoint activeBreakpoint;
571+
final bool isMobile;
572+
final bool isPhone;
573+
final bool isTablet;
574+
final bool isDesktop;
575575

576576
/// Creates responsive data with explicit values.
577577
///
578578
/// Consider using [ResponsiveWrapperData.fromResponsiveWrapper]
579579
/// to create data based on the [ResponsiveWrapper] state.
580580
const ResponsiveWrapperData({
581-
this.screenWidth,
582-
this.screenHeight,
583-
this.scaledWidth,
584-
this.scaledHeight,
585-
this.breakpoints,
586-
this.breakpointSegments,
587-
this.activeBreakpoint,
588-
this.isMobile,
589-
this.isPhone,
590-
this.isTablet,
591-
this.isDesktop,
581+
this.screenWidth = 0,
582+
this.screenHeight = 0,
583+
this.scaledWidth = 0,
584+
this.scaledHeight = 0,
585+
this.breakpoints = const [],
586+
this.breakpointSegments = const [],
587+
this.activeBreakpoint = const ResponsiveBreakpoint.tag(0, name: ''),
588+
this.isMobile = false,
589+
this.isPhone = false,
590+
this.isTablet = false,
591+
this.isDesktop = false,
592592
});
593593

594594
/// Creates data based on the [ResponsiveWrapper] state.
@@ -624,7 +624,7 @@ class ResponsiveWrapperData {
624624
', scaledHeight: ' +
625625
scaledHeight.toString() +
626626
', breakpoints: ' +
627-
(breakpoints?.asMap() ?? '').toString() +
627+
breakpoints.asMap().toString() +
628628
', breakpointSegments: ' +
629629
breakpointSegments.toString() +
630630
', activeBreakpoint: ' +
@@ -639,30 +639,29 @@ class ResponsiveWrapperData {
639639
isDesktop.toString() +
640640
')';
641641

642-
bool equals(String? name) => activeBreakpoint!.name == name;
642+
bool equals(String? name) => activeBreakpoint.name == name;
643643

644644
/// Is the [scaledWidth] larger than or equal to [name]?
645645
/// Defaults to false if the [name] cannot be found.
646646
bool isLargerThan(String? name) {
647647
// No breakpoints to match.
648-
if (breakpoints!.length == 0) return false;
648+
if (breakpoints.length == 0) return false;
649649

650650
// Breakpoint is active breakpoint.
651-
if (activeBreakpoint!.name == name) return false;
651+
if (activeBreakpoint.name == name) return false;
652652

653653
// Single breakpoint is active and screen width
654654
// is larger than default breakpoint.
655-
if (breakpoints!.length == 1 &&
656-
screenWidth! >= breakpoints![0].breakpoint) {
655+
if (breakpoints.length == 1 && screenWidth >= breakpoints[0].breakpoint) {
657656
return true;
658657
}
659658
// Find first breakpoint end boundary that is larger
660659
// than screen width. Breakpoint names could be
661660
// chained so perform a full search from largest to smallest.
662-
for (var i = breakpoints!.length - 2; i >= 0; i--) {
663-
if (breakpoints![i].name == name &&
664-
breakpoints![i + 1].name != name &&
665-
screenWidth! >= breakpoints![i + 1].breakpoint) return true;
661+
for (var i = breakpoints.length - 2; i >= 0; i--) {
662+
if (breakpoints[i].name == name &&
663+
breakpoints[i + 1].name != name &&
664+
screenWidth >= breakpoints[i + 1].breakpoint) return true;
666665
}
667666

668667
return false;
@@ -671,19 +670,19 @@ class ResponsiveWrapperData {
671670
/// Is the [screenWidth] smaller than the [name]?
672671
/// Defaults to false if the [name] cannot be found.
673672
bool isSmallerThan(String? name) =>
674-
screenWidth! <
675-
breakpointSegments!
676-
.firstWhere((element) => element!.responsiveBreakpoint.name == name,
673+
screenWidth <
674+
breakpointSegments
675+
.firstWhere((element) => element.responsiveBreakpoint.name == name,
677676
orElse: () => ResponsiveBreakpointSegment(
678677
breakpoint: 0,
679678
segmentType: ResponsiveBreakpointBehavior.TAG,
680-
responsiveBreakpoint: ResponsiveBreakpoint.tag(0, name: '')))!
679+
responsiveBreakpoint: ResponsiveBreakpoint.tag(0, name: '')))
681680
.breakpoint;
682681

683-
ResponsiveBreakpoint? firstBreakpointNamed(String name) => breakpointSegments!
684-
.firstWhere((element) => element!.responsiveBreakpoint.name == name,
682+
ResponsiveBreakpoint? firstBreakpointNamed(String name) => breakpointSegments
683+
.firstWhere((element) => element.responsiveBreakpoint.name == name,
685684
orElse: null)
686-
?.responsiveBreakpoint;
685+
.responsiveBreakpoint;
687686
}
688687

689688
/// Creates an immutable widget that exposes [ResponsiveWrapperData]

0 commit comments

Comments
 (0)