Reputation: 207
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
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