title: Flutter - Dart : wait a forEach ends
body: <p>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").</p>
  
  <p><strong>Here is what I tried :</strong></p>
  
  <pre><code>// Retrieve initial content from Firebase storage
  var data = await FirebaseStorage.instance.ref().child("...").getData(1048576);
  var content = new String.fromCharCodes(data);
  
  // Edit content with each node from Firebase database
  final response = await FirebaseDatabase.instance.reference().child('...').once();
  response.value.forEach((jsonString) async {
       ...
       // cacheManager.getFile returns a Future&lt;File&gt;
       cacheManager.getFile(signedurl).then((file){ 
           // Modify content
           content=content.replaceAll('test',file.path);
       });
  });
  
  // Finally write the file with content
  print("test");
  final localfile = File('index.html');
  await localfile.writeAsString(content);
  </code></pre>
  
  <p><strong>Result :</strong></p>
  
  <p>"test" is shown before the forEach ends.</p>
  
  <p>I found that we can do in Dart (<a href="https://groups.google.com/a/dartlang.org/forum/#!topic/misc/GHm2cKUxUDU" rel="noreferrer">https://groups.google.com/a/dartlang.org/forum/#!topic/misc/GHm2cKUxUDU</a>) :</p>
  
  <blockquote>
    <p>await Future.forEach</p>
  </blockquote>
  
  <p>but in my case if I do : await Future.response.value.forEach  (sounds a bit weird)</p>
  
  <p>then I get :</p>
  
  <blockquote>
    <p>Getter not found: 'response'.
        await Future.response.value.forEach((jsonString) async {</p>
  </blockquote>
  
  <p>How to wait that forEach ends (with "content" modified) before to write the file with new content?</p>
  
  <p>Any idea?</p>