preface

This article will share with you the function of file storage in Flutter. The SDK of Flutter already has an API for file storage. Therefore, the key to using file storage in Flutter is how to obtain the directory stored in your phone and create different files based on the directory path. According to the characteristics of Flutter, we can use a custom channel to obtain the path of the platform’s storeable folder to the Flutter side. It is very simple to implement. This plugin already exists on pub.dartlang.org, named path_provider. Let’s implement the file storage function by introducing this plug-in.

File Storage Usage

The introduction of the plugin

Add path_provider to the pubspec.yaml file, the latest version is 0.4.1, as follows:

dependencies:
  flutter:
    sdk: flutter
  # path_provider plug-inPath_provider: 0.4.1Copy the code

Then run the flutter Packages get command to download the plugin locally.

By looking at the path_provider.dart code in the plug-in, we see that it provides three methods:

  • getTemporaryDirectory()

    Get temporary directories

  • getApplicationDocumentsDirectory()

    Get the application document directory

  • getExternalStorageDirectory()

    Gets the external storage directory

External.getexternalstoragedirectory () method in the code have platform type judgment:

Future<Directory> getExternalStorageDirectory() async {
  if(platform.isios) // Throw new UnsupportedError("Functionality not available on iOS");
  final String path = await _channel.invokeMethod('getStorageDirectory');
  if (path == null) {
    return null;
  }
  return new Directory(path);
}
Copy the code

The iOS platform does not have the concept of an external storage directory. Therefore, you cannot obtain the external storage directory path.

Method of use

1. After the plug-in is introduced into the project, import the path_provider.dart file into the dart file
import 'package:path_provider/path_provider.dart';
Copy the code
2. Obtain a file directory, create a file object based on the directory, and write data to the file
/ / access application document Directory and create a file Directory documentsDir = await getApplicationDocumentsDirectory (); String documentsPath = documentsDir.path; File file = new File('$documentsPath/notes');
    if(! file.existsSync()) { file.createSync(); } writeToFile(context, file, notes); Void writeToFile(BuildContext context, File File, String notes) async { File file1 = await file.writeAsString(notes);if(file1.existsSync()) {
      Toast.show(context, 'Saved successfully'); }}Copy the code
  • The path of file in the iOS emulator is
/Users/... /CoreSimulator/Devices/D44E9E54-2FDD-40B2-A953-3592C1D0BFD8/data/Containers/Data/Application/28644C62-1FFA-422E-8ED6-54A A4E9CBE0C/Documents/notesCopy the code
  • The path of file in the Android emulator is
/data/user/0/com.example.demo/app_flutter/notes
Copy the code
3. Read the data stored in the specified file
  void getNotesFromCache() async {
    Directory documentsDir = await getApplicationDocumentsDirectory();
    String documentsPath = documentsDir.path;

    File file = new File('$documentsPath/notes');
    if(! file.existsSync()) {return; } String notes = await file.readAsString(); // Set data to update UI after reading datasetState(() {
      ...
    });
  }
Copy the code

Write in the last

File storage function is very simple on the basis of path_provider plug-in, so that’s it. In the next article, we will introduce the use of database storage plug-in SQflite, so that you can meet the requirements of batch data persistent storage.

Description:

This article is reprinted from the corresponding “Flutter Programming Guide” wechat official account. For more Flutter related articles, open our wechat and scan our QR code to follow our wechat official account.