我正在使用官方API列出我为某些目的创建的目录,并将少量文件压入其中。现在我想列出来。
这是我首先创造的方式
final Directory extDir = await getApplicationDocumentsDirectory();
// final String dirPath = '${extDir.path}/Movies/flutter_test';
final String dirPath = '${extDir.path}/mydir';
await new Directory(dirPath).create(recursive: true);
这是我在另一页中阅读的内容。
final Directory extDir = await getApplicationDocumentsDirectory();
// final String dirPath = '${extDir.path}/Pictures/flutter_test';
final String dirPath = '${extDir.path}/mydir';
final String thumbDirPath = '$dirPath/thumbs';
final Directory imgDir = Directory.fromUri(Uri.file(dirPath));
dirExists = imgDir.existsSync();
fileCount = 0;
if(dirExists) {
print("my dir exists");
// thumbDir.list(recursive: false, followLinks: false)
fileCount = await imgDir.list(recursive: false).length;
print('mydir images count $fileCount');
if(fileCount > 1) { // we think one is always the directory itself?
try {
imgDir.list(recursive: false, followLinks: false)
.listen((FileSystemEntity entity) {
代码在此处中断,给出以下异常消息
type '() => Null' is not a subtype of type '(Object) => FutureOr<dynamic>'
我还要告诉你,当我列出外部存储目录时,这不会发生。我在创建目录时做错了什么?
编辑
我现在已经将Mac上的颤振升级到0.7.3,并且完全出现了新问题。该应用程序根本不会运行。
好的,我自己找到了解决方案。显然修补大型提供程序API给了我不同的方式来列出项目,实际上告诉我,我试图两次运行监听器。
fileCount = await imgDir.list(recursive: false).length;
和
imgDir.list(recursive: false, followLinks: false)
.listen((FileSystemEntity entity) {
从原始代码不能一个接一个地写,因为第一个完成了监听器。我通过这种方式改变我自己的代码并列出目录来发现这一点。
Stream<FileSystemEntity> files = imgDir.list(recursive: false, followLinks: false);
将根据API添加尽可能多的事件,然后创建常规通用列表
List<FileSystemEntity> entities = await files.toList();
然后使用迭代器或for()循环执行常规迭代
for(FileSystemEntity entity in entities) {
... // every file now is available