Flutter - Dart : wait a forEach ends

后端 未结 3 1956

I try to modify a string using data in each node I retrieve from Firebase database, and then to write a file with the modifed string (called \"content\").

He

3条回答
  •  时光取名叫无心
    2021-01-06 18:07

    If you use for(... in ) instead of forEach you can use async/await

    Future someMethod() async {
      ...
    
      for(final jsonString in response.value) {
         ...
         // cacheManager.getFile returns a Future
         cacheManager.getFile(signedurl).then((file){ 
             // Modify content
             content=content.replaceAll('test',file.path);
         });
      });
    }
    

    With forEach the calls fire away for each jsonString immediately and inside it await works, but forEach has return type void and that can't be awaited, only a Future can.

提交回复
热议问题