Dart:
Reading a text file
How to:
Dart’s core library, dart:io
, provides the necessary functionalities to read text files synchronously or asynchronously. Here’s how to approach both.
Synchronously:
import 'dart:io';
void main() {
var fileName = "path/to/your/textfile.txt";
var file = File(fileName);
// Reading the file synchronously
var contents;
try {
contents = file.readAsStringSync();
print(contents);
} catch (e) {
print('Error reading file: $e');
}
}
Asynchronously:
To avoid blocking the program while the file is being read, especially useful for large files or responsive applications:
import 'dart:io';
void main() async {
var fileName = "path/to/your/textfile.txt";
var file = File(fileName);
try {
String contents = await file.readAsString();
print(contents);
} catch (e) {
print('Error reading file: $e');
}
}
Sample Output:
If your text file contains:
Hello, Dart!
Both of the above methods will output:
Hello, Dart!
Using a Third-Party Library:
For additional features like simplified file operations or enhanced error handling, you might consider third-party libraries such as package:file
. However, as of my last update, using the core dart:io
package directly, as shown above, is the most common and straightforward method for reading text files in Dart.