Skip to content

[cross_file] fixed readAsString decoding in-memory bytes as UTF-16 - #12479

Open
glitchfl wants to merge 2 commits into
flutter:mainfrom
glitchfl:fix-cross-file-utf8-read-as-string
Open

[cross_file] fixed readAsString decoding in-memory bytes as UTF-16#12479
glitchfl wants to merge 2 commits into
flutter:mainfrom
glitchfl:fix-cross-file-utf8-read-as-string

Conversation

@glitchfl

Copy link
Copy Markdown

XFile.fromData(utf8.encode('😀')).readAsString() hands back mojibake

The bytes branch uses String.fromCharCodes which just widens each byte into
its own UTF-16 code unit so anything outside ASCII comes out mangled and the
encoding parameter the method accepts never gets used at all. The file-backed
branch right below it passes encoding through fine, and web already does
readAsBytes().then(encoding.decode) so this is mostly just making native do
what web has been doing all along.

I made the method async while I was in there and that bit is deliberate
rather than cosmetic: encoding.decode can throw on bad input where
fromCharCodes never could and without async that would come out
synchronously instead of as a failed future, which isn't what you get from
either the web version or the _file.readAsString path.

fixes flutter/flutter#165120

AI usgae: I used antigravity (with gemini 3.7 flash)

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the readAsString method in XFile to decode bytes using the provided encoding instead of String.fromCharCodes, fixing an issue where multi-byte characters were incorrectly decoded. It also adds corresponding unit tests for multi-byte characters, non-default encodings, and malformed data. The reviewer suggested avoiding the async keyword in readAsString to prevent unnecessary microtask delays and overhead, recommending the use of Future.sync instead.

Comment thread packages/cross_file/lib/src/types/io.dart Outdated
@glitchfl
glitchfl force-pushed the fix-cross-file-utf8-read-as-string branch from 8c83c4b to c49b2dc Compare August 16, 2026 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cross_file] readAsString assumes bytes are UTF-16

1 participant