PageData<T>.fromJson constructor

PageData<T>.fromJson(
  1. Map<String, dynamic> json,
  2. T callBack(
    1. Map<String, dynamic> json
    ), {
  3. bool safe = true,
})

If safe is true and callback throws and exception, the single element is skipped, otherwise if safe is false, the exception is re-thrown

Implementation

factory PageData.fromJson(Map<String, dynamic> json, T callBack(Map<String, dynamic> json), {bool safe = true}) {
  String? _next;
  String? _previous;

  if (json['next'] is String) _next = json['next'];
  if (json['next'] is int) _next = json['next'] > 0 ? '${json['next']}' : null;
  if (json['next'] is bool) _next = json['next'] ? 'true' : null;

  if (json['previous'] is String) _previous = json['previous'];
  if (json['previous'] is int) _previous = json['previous'] > 0 ? '${json['previous']}' : null;
  if (json['previous'] is bool) _previous = json['previous'] ? 'true' : null;

  return PageData(
    data: List<T?>.generate(
      json['results'].length,
      ((element) {
        try {
          return callBack(json['results'][element]);
        } catch (e) {
          if (kDebugMode) print('[PAGE DATA] Error while parsing json: $json');
          if (safe) return null;
          rethrow;
        }
      }),
    ).whereType<T>().toList(),
    count: json['count'] ?? 0,
    next: _next,
    previous: _previous,
  );
}