FileSystemMoveEvent 无法触发?

问题描述 投票:0回答:1

我找不到触发方式

FileSystemMoveEvent

这是一个简单的代码

final uri1 = Uri.file('junk.file');
final file1 = File.fromUri(uri1).absolute;
final sub = file1.watch().listen(print);

file1.create(); // triggers FileSystemCreateEvent

file1.writeAsString('A'); // triggers FileSystemModifyEvent

final parentUri = Directory.current.uri;
file1.renameSync('${parentUri.path}junk1.file'); // triggers FileSystemDeleteEvent
// or file1.delete(); // triggers FileSystemDeleteEvent

除了移动事件之外的所有事件都可以被触发。我什至尝试重命名父目录,但没有触发移动事件。

那么,是什么触发了

FileSystemMoveEvent

API链接:

  1. https://api.dart.dev/stable/3.5.1/dart-io/FileSystemMoveEvent-class.html
  2. https://api.dart.dev/stable/3.5.1/dart-io/FileSystemEntity/watch.html
dart triggers dart-io filesystem-events
1个回答
0
投票

可以通过监听目录的更改来触发

move
事件:

import 'dart:io';

void main() async {
  final uri = Uri.file('test.file');
  final file = File.fromUri(uri).absolute;
  final directory = Directory.current;

  final steamSubscription = directory.watch().listen(print);

  await file.create(); // triggers FileSystemCreateEvent
  // await file.writeAsString('A'); // triggers FileSystemModifyEvent

  final processResult = await Process.run(
    'mv',
    ['test.file', 'test_bak.file'],
    workingDirectory: directory.path,
  );

  steamSubscription.asFuture();
  steamSubscription.cancel();

  print('Exiting: ${processResult.exitCode}');
}

控制台输出:

$ dart main.dart 
FileSystemCreateEvent('/home/dan/stackoverflow/example/test.file', isDirectory=false)
FileSystemMoveEvent('/home/dan/stackoverflow/example/test.file', isDirectory=false, 
  destination=/home/dan/stackoverflow/example/test_bak.file)
Exiting: 0
© www.soinside.com 2019 - 2024. All rights reserved.