In the preceding code, we are combining the methods contained in two libraries: we're using path_provider to retrieve the documents folder in the device and the dart:io library to create a new file, write content, and read its content.
Now, you might be wondering, why do you need to use path_provider to get the documents folder, and not just write anywhere?
Local drives in iOS and Android are mostly inaccessible to apps as a security measure. Apps can only write to select folders, and those include the temp and documents folders.
When dealing with files, you need to do the following:
- Get a reference to the file.
- Write some content.
- Read the file's content.
We performed those steps in this recipe. Note the following instruction:
myFile = File('$documentsPath/pizzas.txt');
This creates a File object, whose path is specified as a parameter.
Then, we have the following instruction:
await myFile.writeAsString('Margherita, Capricciosa, Napoli&apos...