toString method
Returns the string representation of this CID.
- For CIDv0, this is the Base58btc encoded multihash (e.g., "Qm...").
- For CIDv1, this is the multibase-prefixed, Base32 encoded (lowercase, no padding) representation of the full CID bytes (e.g., "bafy..."). This is the canonical string form for CIDv1.
To get a CIDv1 string in a different base, first get the bytes using toBytes and then encode with the desired multibase implementation.
Implementation
@override
String toString() {
if (version == V0) {
return mb.base58.encode(multihash);
} else {
// CIDv1
// The multibase prefix for base32 is 'b'
// We need a base32 encoder here.
// Canonical CIDv1 strings are base32 encoded (lowercase, no padding).
final cidBytes = toBytes();
// The base32 package usually outputs uppercase and may include padding.
// We need to ensure lowercase and no padding.
String base32Encoded = b32.base32.encode(cidBytes);
base32Encoded = base32Encoded.toLowerCase().replaceAll('=', ''); // Ensure lowercase and remove padding
return 'b$base32Encoded';
}
}