How transform a dart's ByteData into a String? - encoding

I am reading a binary file and want to transform it into a String. How do I do it in Dart?

Try the following
String getStringFromBytes(ByteData data) {
final buffer = data.buffer;
var list = buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
return utf8.decode(list);
}
Also see this answer.

Related

Flutter: How to convert AssetImage to Unit8List?

I am trying to insert to a SQFlite database a webp image that I have in my assets.
But I don't know how to convert the asset image to a Uint8List which is the data type in my DB.
How can I do it?
I have tried this:
Future<Uint8List> convert() async {
final ByteData bytes = await rootBundle.load('assets/ab.webp');
final Uint8List list = bytes.buffer.asUint8List();
return list;
}
Uint8List list = convert();
But I get the following error:
Type: Future Function()
A value of type 'Future' can't be assigned to a variable of type 'Uint8List'.
Try changing the type of the variable, or casting the right-hand type to 'Uint8List'.
Thank you in advance
convert() is a async function so when you want to use it you should await for the result and also your convert function does not rerturn any thing.
try this:
Future<Uint8List> convert() async {
final ByteData bytes = await rootBundle.load('assets/image.webp');
final Uint8List list = bytes.buffer.asUint8List();
return list;
}
then use it like this:
Uint8List list = await convert();

save map locally and use it elsewhere

I'm converting a map to a string in order to save it to the device memory
_read() async {
try {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/answers.txt');
String text = await file.readAsString();
print(text);
} catch (e) {
print("Couldn't read file");
}
}
_save() async {
final directory = await getApplicationDocumentsDirectory();
final file = File('${directory.path}/answers.txt');
await file.writeAsString(answers.toString());
print('saved');
}
now I want to use it as a map to access the data on the map. is there a way to do it?
my map looks like this {Everyone should read...: Harry Potter, Two truths and a lie...: something, I can quote every line from...: the alchemist}
What you want is a JSON file.
JSON is a special syntax that can be used to store maps and lists in a file.
There is a catch though: You may only store maps and lists of primitive values like string, int or bool, a custom class, for example, cannot be stored in a JSON file. You would have to convert it into a map first.
In order to turn a JSON string into a map, you can use the jsonDecode function. Similarly, the jsonEncode function will return a string from a map.
Here goes the code:
Future<Map<String, dynamic>> _read() async {
final file = File(filePath);
final jsonStr = await file.readAsString()
return jsonDecode(jsonStr) as Map<String, dynamic>>;
}
Future<void> _write(Map<String, dynamic> map) async {
final jsonStr = jsonEncode(map);
final file = File(filePath);
await file.writeAsString(jsonStr);
}
In my code I skipped the try-catch block and the Directory thing, that's just to make the example simpler.

flutter get pdf files string data to variable

I want to take a pdf files string data in a fast way and the package which I use is giving some problems.
How can I get correctly and fast way a pdf file?
Thanks for your help.
this is the way ı using
PDFDoc? _pdfDoc;
String _text = "";
_getPdfFile() {
setState(() async {
_pdfDoc = await PDFDoc.fromPath(filePath!);
data = await _pdfDoc!.text;
});
}
this is the source of code. this code will take string data from the pdf page 3.
Instead, if you want all the data in pdf you can look at this link.https://help.syncfusion.com/flutter/pdf/working-with-text-extraction
final String data;
final PdfDocument document =
PdfDocument(inputBytes: myFile.readAsBytesSync());
setState(() {
data = PdfTextExtractor(document).extractText(startPageIndex: 3);
});
document.dispose();

How to use a String returned by a Future<String> method in flutter?

I'm trying to read a json file's components using the following method:
import 'dart:io';
class CharacterDataReader {
Future<String> read() async {
final file = File('assets/data/character_data.json');
String data = await file.readAsString();
return data;
}
}
Now, I'm trying to assign the read values to a String named data and json.decode() it in another class using the following:
Future<String> data = CharacterDataReader().read();
Map<String, dynamic> characterData = json.decode(data);
However, this doesn't work since json.decode() only accepts Strings as a parameter. Therefore, can someone please tell me how do I convert this Future into an actual string?
since its a future you have to add await keyword
String data= await CharacterDataReader().read();
check out dart official doc on asynchronous programming

Flutter: How to encode and decode audio files in Base64 format?

I'm building a ChatBot app using Dialogflow and I want to implement Voice Recognition feature in my app. As you know Dialogflow provide us a feature to detect intent on the basis of audio but it only accepts audio in the form of base64. The problem for me is that I'm unable to encode the audio file into Base64. I'm new to Flutter Development so if in case I'm missing something or doing it in a wrong way then please let me know. Thanks!
I've tried this method but it's not giving me the proper output:
Future<String> makeBase64(String path) async {
try {
if (!await fileExists(path)) return null;
File file = File(path);
file.openRead();
var contents = await file.readAsBytes();
var base64File = base64.encode(contents);
return base64File;
} catch (e) {
print(e.toString());
return null;
}
}
You could do this:
List<int> fileBytes = await file.readAsBytes();
String base64String = base64Encode(fileBytes);
The converted string doesn't include mimetype, so you might need to include like this
final fileString = 'data:audio/mp3;base64,$base64String';