decodeJson method Null safety

void decodeJson(
  1. Map<String, dynamic> json,
  2. {required bool needsSave}
)

Convert from a data structure that comes from JSON. If there's an error in the middle, it might be partially read, but not in a way that causes bad behavior in the calculator.

Implementation

void decodeJson(Map<String, dynamic> json, {required bool needsSave}) {
  if (_jsonVersion != json['version']) {
    throw ArgumentError("Version ${json['version']} unrecognized");
  }
  final String jModelName = (json['modelName'] as String?) ?? '16C';
  // 16C came first, and older 16C versions don't save the model name.
  if (modelName != jModelName) {
    throw ArgumentError(
        'Wrong calculator model.  This is a $modelName, not a $jModelName.');
  }

  settings.decodeJson(json['settings'] as Map<String, dynamic>);
  trigMode = TrigMode.fromJson(json['trigMode']);
  integerSignMode =
      IntegerSignMode.fromJson(json['integerSignMode'] as String);
  final List<dynamic>? ims = json['imaginaryStack'] as List<dynamic>?;
  displayMode = DisplayMode.fromJson(json['displayMode']!, ims != null);
  // displayMode must be set before stack, since setting
  // display mode alters stack
  wordSize = json['wordSize'] as int;
  int i = 0;
  for (final v in json['stack'] as List<dynamic>) {
    _stack[i++] = Value.fromJson(v as String);
  }
  _lastX = Value.fromJson(json['lastX'] as String);
  if (ims == null) {
    _imaginaryStack = null;
  } else {
    _imaginaryStack = List<Value>.filled(4, Value.zero, growable: false);
    i = 0;
    for (final v in ims) {
      _imaginaryStack![i] = Value.fromJson(v as String);
    }
  }
  final imx = json['lastXImaginary'] as String?;
  if (imx == null) {
    _lastXImaginary = null;
  } else {
    _lastXImaginary = Value.fromJson(imx);
  }
  i = 0;
  for (final v in json['flags'] as List<dynamic>) {
    _flags[i++] = v as bool;
  }
  memory.decodeJson(json['memory'] as Map<String, dynamic>);
  _debugLog = null; // Don't restore debug log, even if it was saved
  display.window = 0;
  this.needsSave = needsSave;
}