-
Notifications
You must be signed in to change notification settings - Fork 1
Troubleshooting
Arun Prakash edited this page Nov 29, 2025
·
6 revisions
- Symptom:
FormatException: Invalid size format - Causes:
- Typos or unknown symbols (e.g.,
XB) - JEDEC/IEC mismatch (e.g.,
KiBparsed under SI)
- Typos or unknown symbols (e.g.,
- Fixes:
- Ensure
standardmatches your input:si | iec | jedec - Use full-form words when unsure (e.g.,
megabytes,kibibytes)
- Ensure
-
ByteConverter(-1)or parsing a negative size throwsArgumentError. - Ensure upstream inputs are sanitized before parsing.
- Lowercase
bdenotes bits; uppercaseBdenotes bytes. - Using
useBytes: falseformats values in bits; setuseBytes: trueto force bytes.
-
forceUnitpins the unit (e.g.,MB,MiB,Gb) and disables auto-scaling. - When using bits, byte units are mapped to bit units where possible (e.g.,
KB->Kb).
- Use
separator(e.g.,,) andspacer(e.g., empty string) to control visuals. -
minimumFractionDigits/maximumFractionDigitsoverrideprecisionrounding style.
-
DataRate.parse('1 MB/s', standard: ByteStandard.iec)will throw (unit inconsistent with IEC). - Prefer
DataRate.parse('1 MiB/s', standard: ByteStandard.iec)for IEC.
- Prefer
BigByteConverterfor exact integer math at EB/ZB/YB scales. - Use
parseByteSizeAutoto decide automatically.
- Log the normalized number/string or run with simpler inputs (e.g., remove grouping) to isolate issues.
- Cross-check with tests in
test/for expected behaviors and edge cases.
-
Using size parser for rates:
- Symptom:
FormatExceptionwhen parsing"MB/s"or"Mbps"withByteConverter.parse. - Fix: Use
DataRate.parse('100 MB/s')orDataRate.parse('100 Mbps').ByteConverter.parseis for sizes (no per-second suffix).
- Symptom:
-
Unknown unit for chosen standard:
- Symptom:
Unknown SI unit,Unknown IEC unit, orUnknown unit for JEDEC. - Fix: Pass the correct
standardmatching your input, or change the unit symbol.- Example:
ByteConverter.parse('1.5 GiB', standard: ByteStandard.iec)orByteConverter.parse('1.5 GB', standard: ByteStandard.si).
- Example:
- Symptom:
-
Bits vs Bytes capitalization:
- Symptom:
mb(bits) vsMB(bytes) gives unexpected value or error under a given standard. - Fix: Use the intended case. Lowercase
bis bits, uppercaseBis bytes. For rates, preferDataRate.
- Symptom:
-
JEDEC limitations:
- JEDEC supports
KB/MB/GB/TB; symbols likeKiB/GiBare IEC-only and will fail under JEDEC. - If you need
KiB/MiB/GiB, selectByteStandard.iec.
- JEDEC supports
-
Mixed context tokens:
- Inputs like
"100 GBps"(bytes per second) vs"100 Gbps"(bits per second) represent different magnitudes; ensure you pick the intended symbol and useDataRatefor per-second values.
- Inputs like
The library does not auto-detect SI/IEC/JEDEC, but you can implement light heuristics:
- Prefer IEC if you see an
ibeforeB(e.g.,KiB,MiB,GiB). - If there's a per-second indicator (
/s,ps,bps,Bps), parse as a data rate withDataRate.parse. - If units are
KB/MB/GBwithouti, default to SI unless your domain expects JEDEC (many storage devices). Optionally fall back to JEDEC on SI failure. - For lowercase
bsuffix (kb,mb,gb), that's bits; handle viaDataRatewhere appropriate or ensure you intend a size in bits.
Example heuristic parser for sizes:
import 'package:byte_converter/byte_converter.dart';
ByteConverter parseSizeSmart(String input) {
final t = input.trim();
final lower = t.toLowerCase();
// Reject rates up-front
if (lower.contains('/s') || lower.endsWith('ps') || lower.endsWith('bps')) {
throw FormatException('Looks like a data rate; use DataRate.parse');
}
// IEC detection by iB suffix
final hasIec = RegExp(r'\b([kmgtpezy]i)b\b', caseSensitive: false)
.hasMatch(lower);
if (hasIec) {
return ByteConverter.parse(t, standard: ByteStandard.iec);
}
// Try SI first, then JEDEC as fallback for binary-1000 confusion contexts
try {
return ByteConverter.parse(t, standard: ByteStandard.si);
} catch (_) {
return ByteConverter.parse(t, standard: ByteStandard.jedec);
}
}And for rates:
import 'package:byte_converter/byte_converter.dart';
DataRate parseRateSmart(String input) {
final t = input.trim();
// IEC-like units in rates (e.g., MiB/s) imply IEC
final isIec = RegExp(r'\b([kmgtpezy]i)b/s\b', caseSensitive: false)
.hasMatch(t);
final std = isIec ? ByteStandard.iec : ByteStandard.si;
return DataRate.parse(t, standard: std);
}| Input example | Use this | Standard | Notes |
|---|---|---|---|
1.5 GB |
ByteConverter.parse('1.5 GB') |
SI | Decimal units without i default to SI. |
1.5 GiB |
ByteConverter.parse('1.5 GiB', standard: ByteStandard.iec) |
IEC |
iB indicates IEC (binary). |
100 MB/s |
DataRate.parse('100 MB/s') |
SI | Per-second bytes; no i → SI. |
100 MiB/s |
DataRate.parse('100 MiB/s', standard: ByteStandard.iec) |
IEC |
MiB/s clearly IEC. |
100 Mbps |
DataRate.parse('100 Mbps') |
SI | Lowercase b = bits per second. |
1536 KB |
ByteConverter.parse('1536 KB') |
SI | Will auto-scale when formatted (≈1.5 MB). |
10 Mb |
ByteConverter.parse('10 Mb') |
SI | Bits as size (not rate); for per-second use Mbps. |
1 TB (storage) |
ByteConverter.parse('1 TB', standard: ByteStandard.jedec) |
JEDEC | Many storage specs use JEDEC; choose per domain. |