I have a ".txt" file called "bio.txt" inside the Document folder of my application. I want to read its content and store it inside a String variable. My problem is that the "reading action" is a "Future" function and I have some troubles to save its value in a "String".
I read the file with :
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> _localFile(String name) async {
final path = await _localPath;
return File('$path/$name');
}
Future<String> _read(String filename) async {
try {
final file = await _localFile(filename);
// Read the file.
return await file.readAsString();
} catch (e) {
// If encountering an error, return 0.
return "Can't read";
}
}
And I would like to save it with :
String _bio = _read("bio.txt").then((value) => String);
Do you know why this solution isn't working? Any suggestion?