ResponsiveValue<T> constructor Null safety

ResponsiveValue<T>(
  1. BuildContext context,
  2. {required T defaultValue,
  3. required List<Condition> valueWhen}
)

Implementation

ResponsiveValue(this.context,
    {required this.defaultValue, required this.valueWhen}) {
  // Breakpoint reference check. Verify a parent
  // [ResponsiveWrapper] exists if a reference is found.
  if (valueWhen.firstWhereOrNull((element) => element.name != null) != null) {
    try {
      ResponsiveWrapper.of(context);
    } catch (e) {
      throw FlutterError.fromParts(<DiagnosticsNode>[
        ErrorSummary(
            'A ResponsiveCondition was caught referencing a nonexistant breakpoint.'),
        ErrorDescription(
            'A ResponsiveCondition requires a parent ResponsiveWrapper '
            'to reference breakpoints. Add a ResponsiveWrapper or remove breakpoint references.')
      ]);
    }
  }

  List<Condition> conditions = valueWhen;
  List<ResponsiveBreakpointSegment>? segments =
      ResponsiveWrapper.of(context).breakpointSegments;
  conditions = conditions.map((e) {
    if (e.breakpoint == null) {
      return e.copyWith(
          breakpoint: segments
              .firstWhere(
                  (element) => element.responsiveBreakpoint.name == e.name,
                  orElse: () =>
                      throw ('No breakpoint named `${e.name}` found.'))
              .responsiveBreakpoint
              .breakpoint
              .toInt());
    }
    return e;
  }).toList();
  conditions.sort((a, b) => a.breakpoint!.compareTo(b.breakpoint!));
  // Get visible value from active condition.
  value = getValue(context, conditions) ?? defaultValue;
}