I am creating a quiz app in the Flutter for which I have collected some questions in CSV file. I want to store the CSV file in firebase and display questions into the app by reading from the CSV file. But just to check if the reading file is as simple as it should be, I tried to read a dummy file in this way:
new File('file.txt').readAsString().then((String contents) {
print(contents);
});
from main.dart before returning the Widget. But i get this error:
`FileSystemException: Cannot open file, path = 'file.txt' (OS Error: No such file or directory, errno = 2)`
even though I have made a dummy 'file.txt' file in the same directory as 'main.dart'.
I tried doing './file.txt' and even the absolute path from windows explorer but none seem to work.
How to fix this?
Drawing a line is probably the easiest thing to do with paths. First, move the current point of the path to the starting point using the moveTo function. Then draw the line using the lineTo function to the endpoint. That's it.
Create a new Directory to give access the directory with the specified path: var myDir = Directory('myDir'); Most instance methods of Directory exist in both synchronous and asynchronous variants, for example, create and createSync.
If the files are on different file systems you need to create a new destination file, read the source file and write the content into the destination file, then delete the source file.
The path_provider
package allows you to access the temp and appDir directory
https://pub.dartlang.org/packages/path_provider
Directory tempDir = await getTemporaryDirectory(); String tempPath = tempDir.path; Directory appDocDir = await getApplicationDocumentsDirectory(); String appDocPath = appDocDir.path;
You can use the join()
method of https://pub.dartlang.org/packages/path to concatenate paths in a platform dependent way, or just use string concatenation like
String filePath = '${appDocDir.path}/file.txt';
new File(filePath).readAsString().then((String contents) {
print(contents);
});
You can use join to safely join files or folders:
Example:
import 'package:path/path.dart' as Path;
String fileName = Path.join('/storage/emulated/0/', '/MyFolder/file.txt');
Result:
fileName = '/storage/emulated/0/MyFolder/file.txt'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With