title: How to show a dialog inside a futurebuilder?
body: <p>I want to show a dialog if I receive an error in a futurebuilder. </p>
  
  <p>If I receiver an error, I want to show a dialog and force the user to click on the button, so that he can be redirected to another page.</p>
  
  <p>The problems seems to be that it is not possible to show a dialog while widget is being built.</p>
  
  <pre><code>FutureBuilder(
        future: ApiService.getPosts(),
        builder: (BuildContext context, AsyncSnapshot snapShot) {
          if (snapShot.connectionState == ConnectionState.done) {
            if (snapShot.data.runtimeType == http.Response) {
              var message =
                json.decode(utf8.decode(snapShot.data.bodyBytes));
  
      showDialog(
       context: context,
       barrierDismissible: false,
       builder: (context) {
         return AlertDialog(
          content: Text(message),
          actions: &lt;Widget&gt;[
            FlatButton(
              child: Text("Ok"),
              onPressed: () =&gt; null",
            )
          ],
        );
      });
    }
       return ListView.separated(
              separatorBuilder: (BuildContext context, int index) {
                return Divider(
                  color: Colors.grey,
                  height: 1,
                );
              },
              itemBuilder: (BuildContext context, int index) {
                return _buildPostCard(index);
              },
              itemCount: snapShot.data.length,
            );
       return Center(
            child: CircularProgressIndicator(),
          );
        },
      )
  </code></pre>
  
  <p>If I return the AlertDialog alone, it works. But I need the showDialog because of the barrierDismissible property.</p>
  
  <p>Does any one know if that is possible?
  Also, is this a good way to handle what I want?</p>
  
  <p>Thanks</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>For further reference, a friend at work had the solution.</p>
  
  <p>In order to do what I was looking for, I had to decide which future I was going to pass to the futureBuilder. </p>
  
  <pre><code>Future&lt;List&lt;dynamic&gt;&gt; getPostsFuture() async { 
  try {
      return await ApiService.getPosts();
    } catch (e) {
  
       await showDialog(
   context: context,
   barrierDismissible: false,
   builder: (context) {
     return AlertDialog(
      content: Text(message),
      actions: &lt;Widget&gt;[
        FlatButton(
          child: Text("Ok"),
          onPressed: () =&gt; null",
        )
      ],
    );
  });
    }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Then in the futureBuilder I would just call</p>
  
  <pre><code>FutureBuilder(
    future: getPostsFuture(),
  </code></pre>
  
  <p>Thanks</p>