dispatch_source_watcher 0.1.0
dispatch_source_watcher: ^0.1.0 copied to clipboard
Listen to DispatchSource file vents on iOS
example/lib/main.dart
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:dispatch_source_watcher/dispatch_source_watcher.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final watcher = DispatchSourceWatcher();
@override
void initState() {
super.initState();
watcher.initialize();
watcher.watch("/tmp/dir1", onFsEvent);
Directory("/tmp/dir1").createSync();
}
@override
void dispose() {
watcher.cancelWatchCallback("/tmp/dir1", onFsEvent);
watcher.dispose();
super.dispose();
}
void onFsEvent(DispatchSourceEvent event) {
print("DBG flutter app got event: ${event.path}, ${event.eventNames}");
}
void writeFile() {
final test2 = File('/tmp/dir1/test2');
if (test2.existsSync()) {
test2.delete();
}
File('/tmp/dir1/test2').writeAsStringSync('hello world');
Directory('/tmp/dir1').deleteSync(recursive: true);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: TextButton(
child: Text('create file in /tmp/dir1/'),
onPressed: writeFile
)
),
),
);
}
}