Dice.fromJson constructor

Dice.fromJson(
  1. String json
)

Implementation

factory Dice.fromJson(String json) {
  final matches = _diceMatches(json);
  final amount = int.tryParse(matches[0]!);
  final sides = int.tryParse(matches[1]!);
  final modifierSign = matches[2];
  final modifierValue = matches[3] != null ? int.tryParse(matches[3]!) : null;
  final modifierStat = matches[3] != null && modifierValue == null
      ? matches[3]!.toUpperCase()
      : null;

  if (sides == null || amount == null) {
    throw Exception("Dice parsing failed");
  }

  return Dice(
    amount: amount,
    sides: sides,
    modifierValue:
        modifierSign == '-' ? -(modifierValue ?? 0) : modifierValue,
    modifierSign: modifierSign,
    modifierStat: modifierStat,
  );
}