Skip to content

[Windows] Clipboard.setData stops copy once malformed character appears in the String #162226

Closed

Description

@krll-kov
import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

Future<void> main() async {
  runApp(const MyApp());

  final Uint8List readFile =
      await File(r'C:\Windows\System32\vcruntime140_1.dll').readAsBytes();

  final String tryUtf8 = utf8.decode(readFile, allowMalformed: true);
  print('UTF8 File as String: $tryUtf8');

  final String charcodes = String.fromCharCodes(readFile);
  print('Charcodes File as String: $tryUtf8');

  await Clipboard.setData(ClipboardData(text: tryUtf8));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

File content that we read with utf8

Image

What is copied to clipboard

Image

File content that we read with charcodes

Image

What is copied to clipboard

Image

Flutter doctor -v

PS C:\Users\swiftsoft\Desktop\test_project> flutter doctor -v
[√] Flutter (Channel stable, 3.27.3, on Microsoft Windows [Version 10.0.26100.2605], locale en-US)
    • Flutter version 3.27.3 on channel stable at c:\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c519ee916e (5 days ago), 2025-01-21 10:32:23 -0800
    • Engine revision e672b006cb
    • Dart version 3.6.1
    • DevTools version 2.40.2

[√] Windows Version (Installed version of Windows is version 10 or higher)

[X] Android toolchain - develop for Android devices
    • Android SDK at C:\Users\swiftsoft\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.

[X] Chrome - develop for the web (Cannot find Chrome executable at .\Google\Chrome\Application\chrome.exe)
    ! Cannot find Chrome. Try setting CHROME_EXECUTABLE to a Chrome executable.

[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.11.5)
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Community
    • Visual Studio Community 2022 version 17.11.35327.3
    • Windows 10 SDK version 10.0.22621.0

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/to/windows-android-setup for detailed instructions).

[√] IntelliJ IDEA Community Edition (version 2024.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2024.2.4
    • Flutter plugin version 83.0.4
    • Dart plugin version 243.23177

[√] Connected device (2 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.26100.2605]
    • Edge (web

Activity

  1. danagbemava-nc commented on Jan 28, 2025

    @danagbemava-nc
    Member

    Hi @krll-kov, curious, does this still occur if you read the string directly without reading from a file first? Assuming you just copy the output from the console and try to set that in the clipboard, does it still cut off?

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 28, 2025
  3. krll-kov commented on Jan 28, 2025

    @krll-kov
    ContributorAuthor

    Hi @krll-kov, curious, does this still occur if you read the string directly without reading from a file first? Assuming you just copy the output from the console and try to set that in the clipboard, does it still cut off?

    You can not read file as sting since it has malformed characters(for utf8 decoder), as for copy, you can copy it from console/notepad without any problems (manually, without flutter)

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 28, 2025
  5. krll-kov commented on Jan 28, 2025

    @krll-kov
    ContributorAuthor

    And if you mean to create a var with manually copied string and try to copy it, then yes, it works

    Image Image
  6. danagbemava-nc commented on Jan 29, 2025

    @danagbemava-nc
    Member

    And if you mean to create a var with manually copied string and try to copy it, then yes, it works

    Yes, this is what I meant. Does this work as expected on the web? I want to confirm if this is specific to the windows platform or if it reproduces on other platforms as well. It would help if we could get a sample that doesn't rely on a windows specific file.

    Thank you

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 29, 2025
  8. krll-kov commented on Jan 29, 2025

    @krll-kov
    ContributorAuthor

    And if you mean to create a var with manually copied string and try to copy it, then yes, it works

    Yes, this is what I meant. Does this work as expected on the web? I want to confirm if this is specific to the windows platform or if it reproduces on other platforms as well. It would help if we could get a sample that doesn't rely on a windows specific file.

    Thank you

    I do not know, I'm windows developer and have no idea of it's possible to read files on web, please,check that manually

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jan 29, 2025
  10. danagbemava-nc commented on Jan 30, 2025

    @danagbemava-nc
    Member

    I use the file_selector plugin to select some files that should contain some malformed characters and tested the sample on windows and macOS. On macOS, the clipboard appeared to retain all data but on windows, it got cut off after a certain point.

    Steps to reproduce

    • Run the sample code below
    • Click on the fab
    • Select the cache.dill.track.dill file from the build folder and wait for the logs to be printed (at this point, the clipboard should have the data) On windows, you can just select the dll file from the OP
    • Attempt to paste what is in the clipboard

    Reproduces on stable & master

    complete sample
    import 'dart:convert';
    import 'dart:io';
    
    import 'package:file_selector/file_selector.dart';
    import 'package:flutter/material.dart';
    import 'package:flutter/services.dart';
    
    void main() {
      runApp(const MainApp());
    }
    
    class MainApp extends StatelessWidget {
      const MainApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: HomeScreen(),
        );
      }
    }
    
    class HomeScreen extends StatefulWidget {
      const HomeScreen({
        super.key,
      });
    
      @override
      State<HomeScreen> createState() => _HomeScreenState();
    }
    
    class _HomeScreenState extends State<HomeScreen> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          floatingActionButton: FloatingActionButton(
            onPressed: () async {
    
            final file = await openFile();
    
              if (file != null) {
                final Uint8List readFile = await File(file.path).readAsBytes();
    
                final String tryUtf8 = utf8.decode(readFile, allowMalformed: true);
                print('UTF8 File as String: $tryUtf8');
    
                final String charcodes = String.fromCharCodes(readFile);
                print('Charcodes File as String: $tryUtf8');
    
                await Clipboard.setData(ClipboardData(text: tryUtf8));
              }
            },
            child: Icon(Icons.save),
          ),
        );
      }
    }
    [√] Flutter (Channel master, 3.29.0-1.0.pre.164, on Microsoft Windows [Version 10.0.22631.4751], locale en-US) [7.4s]
        • Flutter version 3.29.0-1.0.pre.164 on channel master at C:\Users\Deana\fvm\versions\master
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 7313c87346 (4 hours ago), 2025-01-29 23:52:56 -0500
        • Engine revision 7313c87346
        • Dart version 3.8.0 (build 3.8.0-24.0.dev)
        • DevTools version 2.42.0
    
  11. added
    engineflutter/engine related. See also e: labels.
    platform-windowsBuilding on or for Windows specifically
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-windowsOwned by the Windows platform team
    and removed
    in triagePresently being triaged by the triage team
    on Jan 30, 2025
  12. added
    P3Issues that are less important to the Flutter project
    triaged-windowsTriaged by the Windows platform team
    on Jan 30, 2025
  13. github-actions commented on Aug 5, 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.

  14. locked as resolved and limited conversation to collaborators on Aug 5, 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

    P3Issues that are less important to the Flutter projectengineflutter/engine related. See also e: labels.found in release: 3.27Found to occur in 3.27found in release: 3.29Found to occur in 3.29has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-windowsBuilding on or for Windows specificallyteam-windowsOwned by the Windows platform teamtriaged-windowsTriaged by the Windows platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions