[cross_file] fixed readAsString decoding in-memory bytes as UTF-16 - #12479
Open
glitchfl wants to merge 2 commits into
Open
[cross_file] fixed readAsString decoding in-memory bytes as UTF-16#12479glitchfl wants to merge 2 commits into
readAsString decoding in-memory bytes as UTF-16#12479glitchfl wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
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.
glitchfl
force-pushed
the
fix-cross-file-utf8-read-as-string
branch
from
August 16, 2026 14:40
8c83c4b to
c49b2dc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
XFile.fromData(utf8.encode('😀')).readAsString()hands back mojibakeThe bytes branch uses
String.fromCharCodeswhich just widens each byte intoits own UTF-16 code unit so anything outside ASCII comes out mangled and the
encodingparameter the method accepts never gets used at all. The file-backedbranch right below it passes
encodingthrough fine, and web already doesreadAsBytes().then(encoding.decode)so this is mostly just making native dowhat web has been doing all along.
I made the method
asyncwhile I was in there and that bit is deliberaterather than cosmetic:
encoding.decodecan throw on bad input wherefromCharCodesnever could and withoutasyncthat would come outsynchronously instead of as a failed future, which isn't what you get from
either the web version or the
_file.readAsStringpath.fixes flutter/flutter#165120
AI usgae: I used antigravity (with gemini 3.7 flash)