Skip to content

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

Closed
flutter/packages
#12479

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!

Activity

  1. maheshj01 commented on Mar 13, 2025

    @maheshj01
    Member

    Reproducible on latest stable and the master channel across all platforms except the web

    import 'dart:convert';
    
    import 'package:cross_file/cross_file.dart';
    import 'package:flutter/material.dart';
    
    Future<void> main() async {
      final String emojisToEncode = "😀 🔥 🚀 🦋 👋🏻";
      final String symbolsToEncode = "© ® ™";
      final file = XFile.fromData(utf8.encode(emojisToEncode), name: 'emojis.txt');
      final file2 =
          XFile.fromData(utf8.encode(symbolsToEncode), name: 'symbols.txt');
    
      print(await file.readAsString()); // should print 😀 but it doesn't.
      print(await file2.readAsString()); 
      runApp(const MyApp());
    }
    mahesh@MacBook-Pro-10 sample_app % flutterm run -d macOS 
    Could not load custom device from config index 0: Expected enabled to be a boolean.
    Launching lib/main.dart on macOS in debug mode...
    --- xcodebuild: WARNING: Using the first of multiple matching destinations:
    { platform:macOS, arch:arm64, id:00008132-0009284E1E39001C, name:My Mac }
    { platform:macOS, arch:x86_64, id:00008132-0009284E1E39001C, name:My Mac }
    Building macOS application...                                           
    ✓ Built build/macos/Build/Products/Debug/sample_app.app
    [IMPORTANT:flutter/shell/platform/embedder/embedder_surface_metal_impeller.mm(50)] Using the Impeller rendering backend (Metal).
    2025-03-13 19:28:05.843 sample_app[71666:892695] +[IMKClient subclass]: chose IMKClient_Modern
    2025-03-13 19:28:05.843 sample_app[71666:892695] +[IMKInputSession subclass]: chose IMKInputSession_Modern
    flutter: ð ð¥ ð ð ðð»
    flutter: © ® â
                    ¢
    Syncing files to device macOS...                                   125ms
    
    Flutter run key commands.
    r Hot reload. 🔥🔥🔥
    R Hot restart.
    h List all available interactive commands.
    d Detach (terminate "flutter run" but leave application running).
    c Clear the screen
    q Quit (terminate the application on the device).
    
    flutter doctor -v
    [!] Flutter (Channel stable, 3.29.1, on macOS 15.1.1 24B2091 darwin-arm64, locale en-US) [834ms]
        • Flutter version 3.29.1 on channel stable at /Users/mahesh/Development/flutter
        ! Warning: `flutter` on your path resolves to /Users/mahesh/Development/flutter_master/bin/flutter, which is not inside your current Flutter
          SDK checkout at /Users/mahesh/Development/flutter. Consider adding /Users/mahesh/Development/flutter/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/mahesh/Development/flutter_master/bin/dart, which is not inside your current Flutter SDK
          checkout at /Users/mahesh/Development/flutter. Consider adding /Users/mahesh/Development/flutter/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 09de023485 (13 days ago), 2025-02-28 13:44:05 -0800
        • Engine revision 871f65ac1b
        • Dart version 3.7.0
        • DevTools version 2.42.2
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks
          and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [2.6s]
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-35, build-tools 35.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /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.5+-12932927-b750.29)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.0) [1,220ms]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16A242d
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [144ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [143ms]
        • Android Studio at /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.5+-12932927-b750.29)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1) [142ms]
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.96.2) [20ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.102.0
    
    [✓] Connected device (4 available) [6.7s]
        • iPhone 16 Pro (mobile)          • 34497AEA-B6B0-4F79-99A3-6E40007745A3 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-18-0
          (simulator)
        • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 15.1.1 24B2091 darwin-arm64
        • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 15.1.1 24B2091 darwin-arm64
        • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 131.0.6778.265
    
    [✓] Network resources [432ms]
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
        • Flutter version 3.30.0-1.0.pre.560 on channel master at /Users/mahesh/Development/flutter_master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 25e5ffd8c2 (2 days ago), 2025-03-11 19:27:32 -0400
        • Engine revision 25e5ffd8c2
        • Dart version 3.8.0 (build 3.8.0-171.0.dev)
        • DevTools version 2.44.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0) [2.1s]
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-35, build-tools 35.0.0
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /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.5+-12932927-b750.29)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.0) [1,381ms]
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 16A242d
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [145ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [144ms]
        • Android Studio at /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.5+-12932927-b750.29)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1) [139ms]
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.96.2) [21ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.102.0
    
    [✓] Connected device (3 available) [6.6s]
        • iPhone 16 Pro (mobile) • 34497AEA-B6B0-4F79-99A3-6E40007745A3 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-18-0 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 15.1.1 24B2091 darwin-arm64
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 131.0.6778.265
    
    [✓] Network resources [300ms]
        • All expected network resources are available.
    
    • No issues found!
    
    
  2. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    and removed
    in triagePresently being triaged by the triage team
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Mar 13, 2025
  3. BirjuVachhani commented on Mar 15, 2025

    @BirjuVachhani
    Author

    Why is it doing String.fromCharCodes(_bytes!) if bytes are provided?

  4. changed the title [-][cross_file] Reading data back from a populated file breaks with Emojis.[/-] [+][cross_file] `readAsString` assumes bytes are UTF-16[/+] on Mar 17, 2025
  5. stuartmorgan-g commented on Mar 17, 2025

    @stuartmorgan-g
    Contributor

    fromCharCodes expects UTF-16 input; we should probably either detect the encoding, or make it a constructor param.

  6. added
    P2Important issues not at the top of the work list
    on Mar 17, 2025
  7. BananaMasterz commented on Apr 24, 2025

    @BananaMasterz
  8. BirjuVachhani commented on Apr 25, 2025

    @BirjuVachhani
    Author

    @BananaMasterz Your issue doesn't seem to be related to this package in any way.

  9. raju-muliyashiya commented on Nov 11, 2025

    @raju-muliyashiya

    @stuartmorgan-g What can we do after we detect encoding?

  10. github-actions commented on Sep 8, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  11. locked as resolved and limited conversation to collaborators on Sep 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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