gpt4 book ai didi

dart - 无限文件观察者

转载 作者:行者123 更新时间:2023-12-03 03:10:49 25 4
gpt4 key购买 nike

您好我正在尝试在 Dart 中实现文件观察器。问题是我不知道如何为流制作永远的列表。在我的代码中 print语句仅在文件更改时触发一次。我试过while(true){}但没有产生影响。

import "dart:io";


void main(){
List<String> paths = ['any.dart'];
paths.forEach((fp){
File f = new File(fp);
f.watch().listen((e){
print(e);
});
});
}

Dart 信息: Dart VM version: 1.4.0 (Tue May 20 04:56:35 2014) on "linux_x64"

最佳答案

请查看 watch 函数的文档:

The implementation uses platform-dependent event-based APIs for receiving file-system notifications, thus behavior depends on the platform. 

* Windows: Uses ReadDirectoryChangesW. The implementation only
supports watching directories. Recursive watching is supported.

* `Linux`: Uses `inotify`. The implementation supports watching both
files and directories. Recursive watching is not supported.
Note: When watching files directly, delete events might not happen
as expected.

* `Mac OS`: Uses `FSEvents`. The implementation supports watching both
files and directories. Recursive watching is supported.

这意味着这在 Windows 上不起作用。对于你的问题,它说:
The returned value is an endless broadcast [Stream], that only stops when
one of the following happends:

* The [Stream] is canceled, e.g. by calling `cancel` on the
[StreamSubscription].
* The [FileSystemEntity] being watches, is deleted.

这意味着要么你的文件被删除,流被取消,要么这是一个错误,你应该为此提交一张票。

在 Windows 上,我没有任何 react 。在 Mac 上,我用你的代码得到这个:
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)

你如何测试你的代码?我简单地编辑并保存了我的文件。所以它对我很有用。您使用的是哪个版本?

问候
罗伯特

关于dart - 无限文件观察者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24076347/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com