Flutter Local Files
If you don’t need to query but you need to store possibly complex objects and lots of data with full-control then this is probably the best way to do it.
Flutter provides a core package ‘dart.io’ to help you with input and output at the device level. Remember that this may be different for different devices (platforms). For example, some of the file details may be different for an Android than iOS. That is why the Platform class is covered below.
The Flutter ‘dart.io’ core package includes Directory and File objects for the purpose of working with Directories and Files. These objects are excellent because they can work both synchronously and asynchronously, allowing you to maintain a responsive app even when dealing with large amounts of data.
However, this package does not tell you how to store the data in the files, what file format to use and how to serialize and deserialize objects into files. That is both good and bad but it requires some work on your part.
Platform
When you are coding with local files and directories, sometimes you need information about the device platform:
Number of processors.
Path separator.
Operating System.
Operating System version.
Local hostname.
Version.
The Platform class exists to provide this information to you.
Path Separator
Very useful when you want to separate elements from the path, such as the directory and the filename.In the example below, I create a ‘Directory’ object and use it to query local files in the ‘Application Documents’ directory. When I do this, I get a list of files and each file has a path, which includes the filename at the end. I parse out the filename by finding the last path file
separator (using Platform.pathSeparator) and calculating the filename
as the rest of the path from there onward.
Directory(_path).listSync().forEach((FileSystemEntity fse) {
String path = fse.path;
if (path.endsWith(".themeColor")) {
int startIndex = path.lastIndexOf(Platform.pathSeparator) + 1;
int endIndex = path.lastIndexOf(".themeColor");
filenameList.add(path.substring(startIndex, endIndex));
}
});