listFromJSON static method

DebtList listFromJSON(
  1. Map<String, dynamic> json
)

Create a list from JSON data.

Implementation

static DebtList listFromJSON(Map<String, dynamic> json) {
  final meta = json['meta'];
  final totalCount = meta['total-count'] as int?;

  final data = json['data'];

  final newEntries = <DebtEntry>[];

  if (data is List<dynamic>) {
    for (final entry in data) {
      if (entry is Map<String, dynamic>) {
        final newEntry = DebtEntry.debtFromJSON(entry);
        newEntries.add(newEntry);
      }
    }
  }

  // Compute the diffs
  var index = 0;
  for (final entryLater in newEntries) {
    if (index + 1 < newEntries.length) {
      final entryEarlier = newEntries[index + 1];
      entryLater.change = entryLater.totalDebt! - entryEarlier.totalDebt!;
    }
    index++;
  }

  final newList = DebtList(totalRows: totalCount, mostRecentList: newEntries);

  return newList;
}