loadLogs method
Loads persisted logs from the NDJSON file.
Implementation
Future<List<HttpRequestLog>> loadLogs() async {
_loading = true;
try {
if (!await _file.exists()) return [];
final content = await _file.readAsString();
if (content.trim().isEmpty) return [];
final lines = const LineSplitter().convert(content);
final logs = <HttpRequestLog>[];
for (final line in lines) {
if (line.trim().isEmpty) continue;
try {
logs.add(HttpRequestLog.fromJson(
json.decode(line) as Map<String, dynamic>,
));
} catch (_) {
// Skip malformed lines.
}
}
// Keep only the most recent entries.
if (logs.length > maxLogs) {
return logs.sublist(logs.length - maxLogs);
}
return logs;
} catch (_) {
return [];
} finally {
_loading = false;
}
}