encodeList/decodeList helper extraheren uit 4 model-klassen #2073

Closed
opened 2026-09-13 08:05:41 +00:00 by brenno · 0 comments
Owner

Probleem

Vier modelklassen hebben exact hetzelfde JSON-lijst-patroon: jsonEncode([for (final x in list) x.toJson()]) en een jsonDecode + .fromJson + filter.

Betrokken bestanden

Bestand encodeList decodeList
models/library_folder.dart 28-29 33-47
models/recent_file.dart 97-98 102-114
models/checklist_template.dart 95-96 100-113
models/storage_connection.dart 75-76 81-98

Voorbeeld (recent_file.dart regels 97-114):

static String encodeList(List<RecentFile> files) =>
    jsonEncode([for (final f in files) f.toJson()]);

static List<RecentFile> decodeList(String? json) {
  if (json == null || json.isEmpty) return const [];
  try {
    final decoded = jsonDecode(json);
    if (decoded is! List) return const [];
    return [
      for (final item in decoded)
        if (item is Map)
          RecentFile.fromJson(Map<String, Object?>.from(item)),
    ].where((f) => f.path.isNotEmpty).toList();
  } catch (e) {
    logWarning('RecentFile.decodeList: onleesbare recente-lijst', e);
    return const [];
  }
}

library_folder.dart regels 27-47 is nagenoeg identiek, alleen de klassenaam en filter verschillen.

Verschillen

De filter-callback verschilt per model: path.isNotEmpty, name.isNotEmpty, name.isNotEmpty || title.isNotEmpty, of fromJson kan null teruggeven bij StorageConnection.

Voorstel

Voeg een generieke helper toe, bijv. in utils/json_list_codec.dart:

String encodeList<T>(List<T> list, Map<String, Object?> Function(T) toJson) =>
    jsonEncode([for (final x in list) toJson(x)]);

List<T> decodeList<T>(
  String? json,
  T? Function(Map<String, Object?>) fromJson, {
  bool Function(T)? keep,
  String label = 'decodeList',
}) {
  if (json == null || json.isEmpty) return const [];
  try {
    final decoded = jsonDecode(json);
    if (decoded is! List) return const [];
    return [
      for (final item in decoded)
        if (item is Map)
          fromJson(Map<String, Object?>.from(item)),
    ].whereType<T>().where(keep ?? (_) => true).toList();
  } catch (e) {
    logWarning('$label: onleesbare lijst', e);
    return const [];
  }
}

Dit is een pure interne refactor zonder UI-impact.

Schatting

Per model ~10-15 regels, totaal ~35-50 regels.

Labels

duplicate, enhancement

## Probleem Vier modelklassen hebben exact hetzelfde JSON-lijst-patroon: `jsonEncode([for (final x in list) x.toJson()])` en een `jsonDecode` + `.fromJson` + filter. ## Betrokken bestanden | Bestand | encodeList | decodeList | |---|---|---| | `models/library_folder.dart` | 28-29 | 33-47 | | `models/recent_file.dart` | 97-98 | 102-114 | | `models/checklist_template.dart` | 95-96 | 100-113 | | `models/storage_connection.dart` | 75-76 | 81-98 | Voorbeeld (`recent_file.dart` regels 97-114): ```dart static String encodeList(List<RecentFile> files) => jsonEncode([for (final f in files) f.toJson()]); static List<RecentFile> decodeList(String? json) { if (json == null || json.isEmpty) return const []; try { final decoded = jsonDecode(json); if (decoded is! List) return const []; return [ for (final item in decoded) if (item is Map) RecentFile.fromJson(Map<String, Object?>.from(item)), ].where((f) => f.path.isNotEmpty).toList(); } catch (e) { logWarning('RecentFile.decodeList: onleesbare recente-lijst', e); return const []; } } ``` `library_folder.dart` regels 27-47 is nagenoeg identiek, alleen de klassenaam en filter verschillen. ## Verschillen De filter-callback verschilt per model: `path.isNotEmpty`, `name.isNotEmpty`, `name.isNotEmpty || title.isNotEmpty`, of `fromJson` kan null teruggeven bij `StorageConnection`. ## Voorstel Voeg een generieke helper toe, bijv. in `utils/json_list_codec.dart`: ```dart String encodeList<T>(List<T> list, Map<String, Object?> Function(T) toJson) => jsonEncode([for (final x in list) toJson(x)]); List<T> decodeList<T>( String? json, T? Function(Map<String, Object?>) fromJson, { bool Function(T)? keep, String label = 'decodeList', }) { if (json == null || json.isEmpty) return const []; try { final decoded = jsonDecode(json); if (decoded is! List) return const []; return [ for (final item in decoded) if (item is Map) fromJson(Map<String, Object?>.from(item)), ].whereType<T>().where(keep ?? (_) => true).toList(); } catch (e) { logWarning('$label: onleesbare lijst', e); return const []; } } ``` Dit is een pure interne refactor zonder UI-impact. ## Schatting Per model ~10-15 regels, totaal **~35-50 regels**. ## Labels duplicate, enhancement
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
LibreKAT/Ocideck#2073
No description provided.