Skip to content

[cross_file] readAsString assumes bytes are UTF-16 #165120

@BirjuVachhani

Description

@BirjuVachhani

Steps to reproduce

If XFile is created with ut8 bytes and then file.readAsString() is called then it returns incorrect output.

Responsible code from cross_file:

  @override
  Future<String> readAsString({Encoding encoding = utf8}) {
    if (_bytes != null) {
      // TODO(kevmoo): Remove ignore and fix when the MIN Dart SDK is 3.3
      // ignore: unnecessary_non_null_assertion
      return Future<String>.value(String.fromCharCodes(_bytes!));
    }
    return _file.readAsString(encoding: encoding);
  }

This happens on IO, haven't tested web.

Expected results

Output

😀

Actual results

Output

�

Code sample

Code sample
// Doesn't Work
void main() async {
  final String text ="😀";
  final file = XFile.fromData(utf8.encode(text));
  print(await file.readAsString()); // should print 😀 but it doesn't.
}

// Works
void main() async {
  final String text ="😀";
  File('emoji.txt').writeAsStringSync(text);
  final file = XFile('emoji.txt');
  print(await file.readAsString()); // should print 😀 and it does.
}

Screenshots or Video

Screenshots / Video demonstration

[Upload media here]

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.1, on macOS 15.3.2 24D81 darwin-arm64, locale en-IN) [1,306ms]
    • Flutter version 3.29.1 on channel stable at /Users/birjuvachhani/.puro/envs/stable/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 09de023485 (13 days ago), 2025-02-28 13:44:05 -0800
    • Engine revision 010c8a806b
    • Dart version 3.7.0 (build 3.7.0-323.0.dev)
    • DevTools version 2.42.0

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [3.5s]
    • Android SDK at /Users/birjuvachhani/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • ANDROID_HOME = /Users/birjuvachhani/Library/Android/sdk
    • Java binary at: /Users/birjuvachhani/Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
      This is the JDK bundled with the latest Android Studio installation on this machine.
      To manually set the JDK path, use: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version OpenJDK Runtime Environment (build 21.0.4+-12422083-b607.1)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 16.2) [2.7s]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 16C5032a
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [102ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.2) [101ms]
    • Android Studio at /Users/birjuvachhani/Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.4+-12422083-b607.1)

[✓] IntelliJ IDEA Community Edition (version 2024.3.4.1) [99ms]
    • IntelliJ at /Users/birjuvachhani/Applications/IntelliJ IDEA Community Edition.app
    • Flutter plugin version 83.0.4
    • Dart plugin version 243.23654.44

[✓] VS Code (version 1.98.1) [9ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.106.0

[✓] Connected device (4 available) [6.7s]
    • Birju’s iPhone (wireless) (mobile) • 00008110-000E35A83A82401E • ios            • iOS 18.3.2 22D82
    • macOS (desktop)                    • macos                     • darwin-arm64   • macOS 15.3.2 24D81 darwin-arm64
    • Mac Designed for iPad (desktop)    • mac-designed-for-ipad     • darwin         • macOS 15.3.2 24D81 darwin-arm64
    • Chrome (web)                       • chrome                    • web-javascript • Google Chrome 134.0.6998.89

[✓] Network resources [583ms]
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.29Found to occur in 3.29found in release: 3.30Found to occur in 3.30has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: cross_fileThe cross_file pluginpackageflutter/packages repository. See also p: labels.team-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions