escapeHydrationKey function
Escapes an item key for embedding in an HTML comment marker.
Keys using [A-Za-z0-9_:.~-] pass through verbatim for readability.
Anything else (spaces, --, <, >, unicode) is base64url-encoded with
a b64: prefix so markers can never break out of the comment.
Implementation
String escapeHydrationKey(String key) {
if (key.isNotEmpty &&
RegExp(r'^[A-Za-z0-9_:.~-]+$').hasMatch(key) &&
!key.contains('--')) {
return key;
}
// UTF-8 first: base64 over raw UTF-16 code units truncates anything
// outside Latin-1 (CJK, emoji), so those keys never matched on hydrate.
return 'b64:${base64Url.encode(utf8.encode(key))}';
}