SagaProgress constructor

SagaProgress({
  1. required int currentMaxUnlockedLevelId,
  2. required Map<int, LevelProgress> levels,
  3. Map<String, dynamic> extra = const {},
  4. int spentStars = 0,
})

Defensively copies levels and extra as unmodifiable maps, so a caller mutating a returned map cannot silently rewrite "persisted" state — it throws instead. This mirrors the inventory repository's List.unmodifiable guarantee. The cost is that the constructor is no longer const.

Throws an ArgumentError when spentStars is negative or above the stars levels have earned. fromJson clamps instead, because a save is data it has to load; a constructor call is code that can be corrected.

Implementation

SagaProgress({
  required this.currentMaxUnlockedLevelId,
  required Map<int, LevelProgress> levels,
  Map<String, dynamic> extra = const {},
  this.spentStars = 0,
})  : levels = Map<int, LevelProgress>.unmodifiable(levels),
      extra = Map<String, dynamic>.unmodifiable(extra) {
  if (spentStars < 0) {
    throw ArgumentError.value(
      spentStars,
      'spentStars',
      'Must not be negative.',
    );
  }
  if (spentStars > 0 && spentStars > totalStars) {
    throw ArgumentError.value(
      spentStars,
      'spentStars',
      'Exceeds the $totalStars stars these levels have earned.',
    );
  }
}