Accessing the filesystem, part 2 – working with directories
- In this section, we'll build upon the previous recipe so that you can read and write to files using Dart's
Directoryclass and thedart:iolibrary.
Getting ready
To follow along with this recipe, you need to have completed the previous recipe.
How to do it...
In this recipe, we will:
- Create a new file inside our device or simulator/emulator.
- Write some text.
- Read the content in the file and show it on the screen.
The beginning code for this recipe is the end of the previous one. The methods for reading and writing to files are included in the dart.io library. Follow these steps:
- At the top of the
main.dartfile, import thedart:iolibrary:
import 'dart:io';
- At the top of the
_MyHomePageStateclass, in themain.dartfile, create two new State variables for the file and its content:
late File myFile;
String fileText='';
- Still in the
MyHomePageStateclass, create a new method...