i am working on a flutter web project where I am trying to work with data from the clipboard.
First of all my code:
code = {}; // Earlier defined as static Map<String, String> code = {"": ""};
ClipboardData? data = await Clipboard.getData("plain");
if (data == null || data.text == null) {
return;
}
print("Data:\n${data.text}");
print("Test:" "T 1\n S 00\nE 01".split("\n").toString()); // Unnecessary, but I wanted to test if I'm sane.
List<String> entries = data.text!.split("\n");
print(entries.length);
print("Entries: ");
print(entries);
entries.forEach((element) {
List<String> split = element.split(" ");
code[split[0]] = split[1];
print("Result: $code");
setState(() {});
It is used in a function, but I don't think the rest is useful nor necessary.
My problem:
The variable entries
returns very weird values.
With this input copied to the clipboard (This is copied from somewhere else on the website):
T 1
S 00
E 01
exact output (with comments) of the function is:
Data: // This first part is completely understandable and right.
T 1
S 00
E 01
Test:[T 1, S 00, E 01] // This makes sense too.
3
Entries: // Now it starts to behave weirdly.
, E 01] // Where is the opening bracket and the first value?
, E: 01} // What's this print it ends like a map, doesn't start properly and what line is printing it?
// If it's the last line, where is the "Result: " part?
Expected output:
Data:
T 1
S 00
E 01
Test:[T 1, S 00, E 01]
3
Entries:
[T 1, S 00 , E 01]
{E: 1, S: 00, E: 01}
Stuff I tried:
- I changed the
Clipboard.getData("plain")
argument to"text"
andClipboard.kTextPlain
, has no impact at all. - I tried other inputs, doesn't help. The last element always stays, everything else is missing.
- I tested if it is a printing bug, no the variable itself is the problem.
- I tried changing the source where I copied the input from, no impact.
- I tried replacing the linebreaks with something else first and the splitting at these characters.
- I tried a different browser.
- I changed flutter channel from
master
tostable
. - I upgraded flutter. (To
Channel master, 2.6.0-12.0.pre.396
) - I ran
flutter doctor -v
everything is green and fine.
CodePudding user response:
Here is what I believe might be happening:
when you press enter on your keyboard, this is what gets added to your string
\n\r
The first \n
means go to the next line, the \r
means move the cursor to the beggining of the line, so when you remove the \n
, you are left with some \r
which move to the beggining of the line when you wish to print the list, and ovewite the previously written code.
Note that this is not necessarily the cause of the error, most consoles know to ignore carriage return (\r
). It would not be a problem normally, however as you said, it is quite the weird error, and I can't think of any way this would be caused by anything that isn't a carriage return.
If this is indeed the cause of this issue, the solution should be quite simple:
String newData = data.text!.replaceAll('\r', '');
List<String> entries = newData.split('\n');