List<int> to string without enlarging the data file - flutter - flutter

I have a compress function like below, which has a return value List<int>
List<int> compressData() {
var stringBytes = utf8.encode(plainText);
return BZip2Encoder().encode(stringBytes);
}
then I want to encrypt the data with salsa20 from the package encrypt, but must be string data as input data, I've tried with toString() but it makes the data 2X bigger, how to solve the problem?

Related

Why is the base64 string not showing completely?

So this is my code
_image1 = File(pickedImage.path);
List<int> imageBytes = _image1.readAsBytesSync();
String base64Image = base64.encode(imageBytes);
_shcpImg = base64Image;
But when I print the string _shcpImg, it just prints a part of the string, because when I copy and paste that base64 into an online converter, it only shows a really tiny piece of the image. So the thing is that the string is not showing completely or somehow the base64 encoder is not working well.
Any suggestions?
From the comments, since you are using VsCode and you can't print the full string (long string)
You can use log from dart: developer,
if the string is REALLY LONG, There is a workaround to fix this, the idea is to divide your long string into small pieces (in the example, 800 length for each piece) using RegExp and then iterate into the result and print each piece.
void printWrapped(String text) {
final pattern = new RegExp('.{1,800}'); // 800 is the size of each chunk
pattern.allMatches(text).forEach((match) => print(match.group(0)));
}

In Flutter, how can I combine data into a string very quickly?

I am gathering accelerometer data from my phone using the sensors package, adding that data to a List<AccelerometerEvent>, and then combining that data into a (csv) String so I can use file.writeAsString() to save this data as a csv file. The problem I am having is that it takes too long to combine the data into a string.
For example:
List length : 28645
Milliseconds to combine into csv string: 113580
Code:
for (AccelerometerEvent event in history) {
dataString = dataString + '${event.timestamp},${event.x},${event.y},${event.z}\n';
}
What would be a more efficient way to do this?
Should I even combine the data into a string, or is there a better way to save this data to a file?
Thanks
Create a file object
write first line with column names, and after that each row (after \n) will be an event
See: FileMode.append
Will add new strings without replacing existing string in file
File file = File('events.csv');
file.writeAsStringSync('TIMESTAMP, X, Y, Z\n', mode: FileMode.append);
for (AccelerometerEvent event in history) {
final x = event.x;
final y = event.y;
final z = event.z;
final timestamp = event.timestamp;
String data = '$timestamp, $x, $y, $z';
file.writeAsStringSync('$data\n', mode: FileMode.append);
}

Flutter - Subscript from json file

I'm using a json file to store my data and I need to show some of it on the screen.
For example, when I make a simple text widget and use this string:
Text("a\u2081")
It shows "a" with a subscripted "1".
The problem is that when I load it from a json file it just shows "a\u2081".
This is how I load the data:
var myData = await rootBundle.loadString("data/my_data.json");
var data = await json.decode(myData.toString());
I'm storing the values in objects:
Text(TestReport[i].text) // where TestReport[i].text = "a\u2081"
Is there a way how to show the subscript?

How to get double from firestore number format in flutter

I found some question to this issue but none of them were for flutter. Basically I'm saving double value data in firestore number format but when the number is rounded for example 130.00 it save it as an integer. Now how can I make it double when retrieving the data. I've got simple model class which populate the data from map but I'm struggling to make it double there
factory Tool.fromMap(Map<String, dynamic> toolData) {
if (toolData == null) {
return null;
}
final double length = toolData['length']; //<-- how to make it double here
final String name = toolData['name'];
...
return Tool(
length: length,
name: name
...);
}
The known approaches doesn't seems to work here like
toolData['length'].toDouble()
UPDATE
Actually it works.. It just doesn't show as an option in android studio
I think parse method of double class could be solution for this.
double.parse(toolData['length'].toString());

FormatException Uint8List convert to string with dart

I am trying to convert a Uint8List to a string using Dart (in a Flutter project).
I am using the Flutter Android USB Serial plugin ( https://github.com/altera2015/usbserial)
The data are coming from a usb device and are returned from the library as a Stream.
If outputed as a string, it looks like:
[255,0,0,0,255....]
When I try:
String newTag = ascii.decode(asyncSnapshot.data);
I get the error :
FormatException:Invalid value in input: 255
I don't know how to solve this issue, my result should be :
"352206000079439"
Try this one;
List<int> list = 'someData'.codeUnits;
Uint8List bytes = Uint8List.fromList(list);
String string = String.fromCharCodes(bytes);
If data has compressed as a blob type.
Uint8List bytes = Uint8List.fromList(tcp_socket_blob_data);
var inflated = zlib.decode(bytes);
var data = utf8.decode(inflated);
More: flutter/dart: How to decompress/inflate zlib binary string in flutter