title: Flutter: Correct approach to get value from Future
body: <p>I have a function which returns images directory path, it performs some additional check like if directory exists or not, then it behaves accordingly.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>Future&lt;String&gt; getImagesPath() async {
     final Directory appDir = await getApplicationDocumentsDirectory();
     final String appDirPath = appDir.path;
  
     final String imgPath = appDirPath + '/data/images';
  
     final imgDir = new Directory(imgPath);
  
     bool dirExists = await imgDir.exists();
  
     if (!dirExists) {
          await new Directory(imgPath).create(recursive: true);
     }
  
     return imgPath;
  
  }
  </code></pre>
  
  <p>This piece of code works as expected, but I'm having issue in getting value from <code>Future</code>. </p>
  
  <p><strong>Case Scenario:</strong>
  I have data stored in local database and trying to display it, inside listview. I'm using <code>FutureBuilder</code>, as explained in this <a href="https://stackoverflow.com/questions/53634172/flutter-fetch-records-from-database-and-display-in-listview-builder">answer</a>. Each data row has an image connected with it (connected means, the image name is stored in db). </p>
  
  <p>Inside <code>Widget build</code> method, I have this code:</p>
  
  <pre><code>@override
  Widget build(BuildContext context) {
   getImagesPath().then((path){
       imagesPath = path;
       print(imagesPath); //prints correct path
     });
   print(imagesPath);   //prints null
  
  return Scaffold(
      //removed
      body: FutureBuilder&lt;List&gt;(
          future: databaseHelper.getList(),
          initialData: List(),
          builder: (context, snapshot) {
            return snapshot.hasData
                ? ListView.builder(
                    itemCount: snapshot.data.length,
                    itemBuilder: (_, int position) {
                      final item = snapshot.data[position];
                      final image = "$imagesPath/${item.row[0]}.jpg";
                      return Card(
                          child: ListTile(
                        leading: Image.asset(image),
                        title: Text(item.row[1]),
                        subtitle: Text(item.row[2]),
                        trailing: Icon(Icons.launch),
                      ));
                    })
                : Center(
                    child: CircularProgressIndicator(),
                  );
          }));
  </code></pre>
  
  <p>}</p>
  
  <p>Shifting <code>return Scaffold(.....)</code> inside <code>.then</code> doesn't work. Because widget build returns nothing. </p>
  
  <p>The other option I found is <code>async/await</code> but at the end, same problem, code available below:</p>
  
  <pre><code>_getImagesPath() async {
      return await imgPath();
  }
  </code></pre>
  
  <p>Calling <code>_getImagesPath()</code> returns <code>Future</code>, instead of actual data.</p>
  
  <p>I beleive there is very small logical mistake, but unable to find it myself.</p>