首页 > 解决方案 > 在 dart 中的字符串内使用双引号会导致不需要的结果

问题描述

FLUTTER 和 IDE 信息(如果您有更多信息,请告诉我)顺便说一句,尽管 Flutter 医生说了什么,我使用的是 Android Studio 4.2.2(2021 年 6 月 24 日版本)。

C:\Users\Administrator>flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 2.2.3, on Microsoft Windows [Version 10.0.19041.1052], locale tr-TR)
[√] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[√] Chrome - develop for the web
[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.9.4)
[!] Android Studio
    X android-studio-dir = C:\ProgramFiles\Android\Android Studio
    X Android Studio not found at C:\ProgramFiles\Android\Android Studio
[√] VS Code (version 1.57.1)
[√] Connected device (3 available)

Dart SDK version: 2.13.4 (stable) (Wed Jun 23 13:08:41 2021 +0200) on "windows_x64"

我想要的是,我正在创建一个字符串以将其用作Process.Run中的参数。我正在使用命令行来触发FastCopy(Windows 中最快的复制工具,并且有一个非常好的队列过程)。我需要在不破坏代码的情况下使用双引号,因为如果不使用双引号(“)打开和关闭,FastCopy 将无法在文件夹名称中有空格的情况下工作

这是示例:

String path : r"C:\Users\Administrator\Desktop\sample.txt"
String output: r"C:\"

// This will not cause any problems because folder names have no space
// This has no double quote
Process.run("cmd",
           ['/c start /wait C:\\Users\\Administrator\\FastCopy\\FastCopy.exe /cmd=force_copy /auto_close /estimate=TRUE $path /to=$output'],
           runInShell: true,);

现在,当我在变量之前和之后放置双引号(“)时:

String path : r"C:\Users\Administrator\Desktop\sample.txt"
String output: r"C:\"

// This has double quote in it
Process.run("cmd",
           ['/c start /wait C:\\Users\\Administrator\\FastCopy\\FastCopy.exe /cmd=force_copy /auto_close /estimate=TRUE "$path" /to="$output"'],
           runInShell: true,);

如果我将参数部分输出为字符串,则结果是预期的输出字符串。控制台显示它应该是什么。当我自己从 cmd 控制台运行它时,它按预期工作。但是当我在颤振中运行它时,它会在字符串之前添加另一个“C:”所以它变成

C:\C:\Users\Administrator\Desktop\profanity_TR.txt

它会引发目录错误。错误来自我链接的 FastCopy.exe。

目前,我使用的是无引号版本并且没有错误。但我真的很想解决这个问题,因为将它与名称中有空格的目录一起使用并且不想遇到此类问题。

标签: stringflutterandroid-studiodart

解决方案


推荐阅读