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
Directory
class and thedart:io
library.
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.dart
file, import thedart:io
library:
import 'dart:io';
- At the top of the
_MyHomePageState
class, in themain.dart
file, create two new State variables for the file and its content:
late File myFile;
String fileText='';
- Still in the
MyHomePageState
class, create a new method...