Dart FileSystemModifyEvent
last modified April 4, 2025
The FileSystemModifyEvent
class in Dart represents file system
modification events. It's part of the dart:html
library for web
applications.
This event is triggered when files or directories are modified in the watched directory. It provides details about the changes that occurred in the file system.
Basic Definition
FileSystemModifyEvent
is an event object for file system changes.
It extends Event
and contains information about modified entries.
Key properties include path
for changed file paths and
type
for the modification type (create, modify, or delete).
Basic FileSystemModifyEvent Usage
This example shows how to listen for file system modification events.
import 'dart:html'; void main() { DirectoryEntry dir; window.requestFileSystem(1024 * 1024).then((fs) { dir = fs.root; dir.onChange.listen((FileSystemModifyEvent event) { print('Change detected in: ${event.path}'); print('Change type: ${event.type}'); }); }); }
We request a file system and set up an event listener on the root directory. The listener prints details when any modification occurs in the file system.
$ dart main.dart Change detected in: /example.txt Change type: modify
Filtering Specific Event Types
This example demonstrates filtering for specific modification types.
import 'dart:html'; void main() { DirectoryEntry dir; window.requestFileSystem(1024 * 1024).then((fs) { dir = fs.root; dir.onChange.listen((FileSystemModifyEvent event) { if (event.type == 'create') { print('New file created: ${event.path}'); } else if (event.type == 'delete') { print('File deleted: ${event.path}'); } }); }); }
We filter events to only handle file creation and deletion. This helps focus on specific changes while ignoring others like modifications.
$ dart main.dart New file created: /test.txt File deleted: /old.txt
Monitoring Specific Files
This example shows how to watch for changes to specific files only.
import 'dart:html'; void main() { DirectoryEntry dir; const targetFile = '/config.json'; window.requestFileSystem(1024 * 1024).then((fs) { dir = fs.root; dir.onChange.listen((FileSystemModifyEvent event) { if (event.path == targetFile) { print('Config file changed: ${event.type}'); // Reload config or take appropriate action } }); }); }
We check the event path against our target file path. This creates a focused watcher that only responds to changes in the specified configuration file.
$ dart main.dart Config file changed: modify
Handling Multiple Changes
This example demonstrates processing multiple changes from a single event.
import 'dart:html'; void main() { DirectoryEntry dir; window.requestFileSystem(1024 * 1024).then((fs) { dir = fs.root; dir.onChange.listen((FileSystemModifyEvent event) { print('Batch changes detected:'); event.changes.forEach((change) { print(' ${change.path} - ${change.type}'); }); }); }); }
Some implementations may batch multiple changes into a single event. We iterate through all changes in the event to handle each modification individually.
$ dart main.dart Batch changes detected: /file1.txt - modify /temp/file2.txt - create
Error Handling in File Monitoring
This example adds error handling to file system monitoring.
import 'dart:html'; void main() { DirectoryEntry dir; window.requestFileSystem(1024 * 1024).then((fs) { dir = fs.root; var subscription = dir.onChange.listen( (FileSystemModifyEvent event) { print('Change: ${event.path}'); }, onError: (e) => print('Error: $e'), cancelOnError: false ); // Later: subscription.cancel(); }).catchError((e) => print('Filesystem error: $e')); }
We add error handlers for both the filesystem request and event listening. The subscription can be cancelled later when monitoring is no longer needed.
$ dart main.dart Change: /update.txt Error: FileSystemError {code: 5, name: "InvalidModificationError"}
Best Practices
- Specific paths: Filter events to only watch relevant files
- Error handling: Always implement error handlers
- Resource cleanup: Cancel subscriptions when done
- Performance: Avoid heavy processing in event handlers
- Batch handling: Be prepared for multiple changes per event
Source
Dart FileSystemModifyEvent Documentation
This tutorial covered Dart's FileSystemModifyEvent class with practical examples showing basic usage, filtering, error handling, and monitoring techniques.
Author
List all Dart tutorials.