Nibedita Pattnaik
Nibedita Pattnaik

Reputation: 207

How to use FileSystemEntity.watch() in flutter?

I'm building a desktop application, mainly for Windows platform. I want to track events like create, delete in a directory using flutter. I came across the FileSystemEntity.watch() method but I'm unable to implement that. Can someone share the full example codebase for watching a directory?

Upvotes: 3

Views: 1059

Answers (1)

Richard Heap
Richard Heap

Reputation: 51692

Just watch the folder to receive a stream of events. Normally, you then listen to the stream to receive each event as it happens.

import 'dart:io';

void main() {
  final tempFolder = File('D:\\temp');
  tempFolder.watch().listen((event) {
    print(event);
  });
}

Prints:

FileSystemCreateEvent('D:\temp\New Text Document.txt')
FileSystemMoveEvent('D:\temp\New Text Document.txt', 'D:\temp\foo.txt')
FileSystemCreateEvent('D:\temp\New folder')
FileSystemMoveEvent('D:\temp\New folder', 'D:\temp\bar')
FileSystemDeleteEvent('D:\temp\bar')
FileSystemDeleteEvent('D:\temp\foo.txt')

for the creation, rename and deletion of a plain file and folder.

(You tagged the question as Flutter Web even though you are on desktop. You obviously cannot use dart:io in web.)

Upvotes: 6

Related Questions