Asynchronous flatter:Future.then()永远不会被调用

Asynchronous flatter:Future.then()永远不会被调用,asynchronous,flutter,dart,future,Asynchronous,Flutter,Dart,Future,我现在正在弗利特玩期货。我有一些异步函数返回一个未来的对象。我在Future对象上使用then()注册了一个侦听器,以便在值出现时立即更新ui。 但是结果是空的,因为then()在从文件系统加载所有注释之前返回 Future<List<Note>> loadNotes() async { NoteService().findAll().then((result) { result.forEach((note) => print(note.title));

我现在正在弗利特玩期货。我有一些异步函数返回一个未来的对象。我在Future对象上使用then()注册了一个侦听器,以便在值出现时立即更新ui。
但是结果是空的,因为then()在从文件系统加载所有注释之前返回

Future<List<Note>> loadNotes() async { 
 NoteService().findAll().then((result) { 

  result.forEach((note) => print(note.title)); //not printing -> result is emtpty... 

 });
}


//NoteService class
Future<List<Note>> findAll() async {
  return noteRepository.findAll();
}


//NoteRepository class
@override
Future<List<Note>> findAll() async {

  final Directory dir = await directory;

  dir.list().toList().then((List<FileSystemEntity> list) async {
    List<String> paths = List();
    list.forEach((entity) => paths.add(entity.path));

    List<File> _files = List();
    paths.forEach((path) => _files.add(File(path)));

    List<Note> notes = await _extractNotes(_files);
    return Future.value(notes);
 });

 return Future.value(List());    
}

Future<List<Note>> _extractNotes(List<File> _files) async {
List<Note> notes = List();
 _files.forEach((file) {
   String content = file.readAsStringSync();
   print('content: ' + content);   //this is getting printed correctly to the console
   Map<String, dynamic> a = jsonDecode(content);
   if(a.containsKey('codeSnippets')) {
     notes.add(SnippetNoteEntity.fromJson(jsonDecode(content)));
   } else {
     notes.add(MarkdownNoteEntity.fromJson(jsonDecode(content)));
   }
 });
 return Future.value(notes);
Future loadNotes()异步{
NoteService().findAll().then((结果){
result.forEach((note)=>print(note.title));//不打印->结果为emtpty。。。
});
}
//NoteService类
Future findAll()异步{
return noteRepository.findAll();
}
//NoteRepository类
@凌驾
Future findAll()异步{
最终目录dir=等待目录;
dir.list().toList().then((列表)异步{
列表路径=列表();
list.forEach((entity)=>path.add(entity.path));
List _files=List();
paths.forEach((path)=>\u files.add(File(path));
列表注释=等待提取注释(\u文件);
返回未来价值(附注);
});
返回Future.value(List());
}
Future\u extractNotes(列表文件)异步{
列表注释=列表();
_files.forEach((文件){
字符串内容=file.readAsStringSync();
打印('content:'+content);//这将正确打印到控制台
地图a=jsonDecode(内容);
if(a.containsKey('code snippets')){
add(SnippetNoteEntity.fromJson(jsonDecode(content));
}否则{
add(MarkdownNoteEntity.fromJson(jsonDecode(content));
}
});
返回未来价值(附注);

}

可能会抛出错误。您是否在后面添加了catchError(…)。然后(…)?我编辑了我的问题。然后()被调用,但调用太早。它应该在findAll()完成后被调用。因此,返回由
返回的Future,然后返回
方法,现在您返回的是空列表您没有对由
dir.list().toList()返回的Future执行任何操作。然后(…)
。要么等待它,要么归还它(或者两者兼而有之,见鬼!)哦,哇。。。。。不知怎么的,我错过了^^