countRecords method
Streams the file counting newlines — no JSON decode (~1 s per 300 MB). Returns the number of RECORDS (lines minus the header line).
Implementation
Future<int> countRecords() async {
final info = await stat();
if (info == null || info.size == 0) return 0;
var newlines = 0;
var offset = 0;
const block = 1 << 20;
var lastByte = -1;
while (offset < info.size) {
final end = (offset + block) < info.size ? offset + block : info.size;
final bytes = await _readRange(offset, end);
for (final b in bytes) {
if (b == 0x0A) newlines++;
}
if (bytes.isNotEmpty) lastByte = bytes.last;
offset = end;
}
var lines = newlines;
if (lastByte != -1 && lastByte != 0x0A) lines++; // torn final line
return lines > 0 ? lines - 1 : 0; // minus the header line
}