Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 232
Nibedita Pattnaik Avatar asked Dec 05 '25 14:12

Nibedita Pattnaik


1 Answers

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.)

like image 83
Richard Heap Avatar answered Dec 07 '25 03:12

Richard Heap