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?
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With