title: Flutter/Dart: getting directory contents with future not working
body: <p>I'm trying to get the contents of a directory and display it on the screen.</p>
  <p>When I only have 1 future (and hardcode the return values in the methods) I am able to display them. But when I embed a future within a future (which I need to do to get the application directory and then the list of files), it doesn't work.</p>
  <p>Here is my code:</p>
  <pre><code>  Future&lt;List&lt;CardFileInfo&gt;&gt; _getFilelist() {
      var localFileHelper = new LocalFileHelper();
      return Future.value(localFileHelper.getFileList());
    }
  
    @override
    Widget build(BuildContext context) {
  
      return Scaffold(
        appBar: AppBar(
          title: Text(&quot;Cards&quot;)
        ),
         body: Center(
           child: Column(
             children: &lt;Widget&gt;[
               FutureBuilder&lt;List&lt;CardFileInfo&gt;&gt;(
                 future: _getFilelist(),
                 builder: (context, snapshot) {
                   if (snapshot.hasData) {
                      return Text(&quot;Files Found&quot;);
                   }
                   else {
                      return Text(&quot;Still looking&quot;);
                   }
                 } //end of builder
               ), //end of future builder
             ], //end of children
           ), //end of column
         ), //end of center
      ); //end of scaffold
    } 
  </code></pre>
  <p>Here is the code of the localFileHelper.getFileList() method:</p>
  <pre><code>  List&lt;CardFileInfo&gt; getFileList() {
  
      List&lt;CardFileInfo&gt; cardInfoList = [];
      final dirName = getApplicationDocumentsDirectory();
  
      // *** if I uncomment the following two lines, the process works *** 
      // cardInfoList.add(CardFileInfo(&quot;dummy.tsv&quot;));
      // return cardInfoList;
  
      dirName.then((dir) { // &lt;---------- problem seems occurs here with the future
        final files = dir.list().toList();
        files.then((values){
          values.forEach((element) {
            var type = element.path.toString().split(&quot;.&quot;).last.trim();
            if (type == &quot;tsv&quot;) {
              var currCard = CardFileInfo(element.path.toString().trim());
              cardInfoList.add(currCard);
            }
          }); //end of foreEach
          return cardInfoList;
        }); //end of files.then
      }); //end of dirName.then
    } //end of getFileList()
  </code></pre>
  <p>When I step through the code, the &quot;return Text(&quot;Still looking&quot;);&quot; line executes at most twice.</p>
  <p>The function eventually returns the value, but it seems as if the FutureBuilder doesn't wait long enough for the value to return.</p>
  <p>I'm new to Flutter and Dart, and this issue has really perplexed me.</p>