UniformValue.fromMap constructor

UniformValue.fromMap(
  1. Map<String, dynamic> map
)

Parses a UniformValue from a loosely typed map.

Missing keys use defaults:

  • min: 0.0
  • max: 1.0
  • defaultValue: 0.0
  • value: defaultValue

Throws FormatException when types are invalid or constraints fail.

Implementation

factory UniformValue.fromMap(Map<String, dynamic> map) {
  try {
    double toDouble(dynamic input, String key, double fallback) {
      if (input == null) return fallback;
      if (input is num) return input.toDouble();
      throw FormatException('UniformValue.$key must be a number, got ${input.runtimeType}');
    }

    final min = toDouble(map['min'], 'min', 0.0);
    final max = toDouble(map['max'], 'max', 1.0);
    final defaultValue = toDouble(map['defaultValue'], 'defaultValue', 0.0);
    final value = toDouble(
      map['value'],
      'value',
      defaultValue,
    );

    if (min > max) {
      throw FormatException('UniformValue min must be <= max (min=$min, max=$max)');
    }
    if (defaultValue < min || defaultValue > max) {
      throw FormatException(
        'UniformValue defaultValue must be within range [$min, $max], got $defaultValue',
      );
    }
    if (value < min || value > max) {
      throw FormatException(
        'UniformValue value must be within range [$min, $max], got $value',
      );
    }

    return UniformValue(
    defaultValue: defaultValue,
    min: min,
    max: max,
    value: value,
    );
  } catch (e) {
    throw FormatException('Invalid UniformValue data: $e');
  }
}