Flutter file exists

Checking if a file exists in Flutter

In Flutter, you can check if a file exists using the File class from the ‘dart:io’ package. The File class provides various methods to interact with files in the device’s file system, including checking for existence.

Here’s an example of how to check if a file exists in Flutter:


import 'dart:io';

void main() {
  String filePath = '/path/to/file.txt';
  File file = File(filePath);

  file.exists().then((exists) {
    if (exists) {
      print('File exists.');
    } else {
      print('File does not exist.');
    }
  });
}
  

In this example, we first create a File instance with the desired file path. We then call the exists() method on the file, which returns a Future indicating whether the file exists or not. We can use the then() method to handle the result of the future, where we check the boolean value and print the appropriate message.

Make sure to import the ‘dart:io’ package at the top of your file to use the File class.

Leave a comment