title: Flutter : Show an Alert Dialog after an async Api call
body: <p>This is the code to get the Login response. If there is an error I want to show an Alert dialog saying that there was an error during Login.</p>
  
  <pre><code>Future&lt;String&gt; login(String username, String password) async {
      Map&lt;String, dynamic&gt; params = {
         'username': username,
         'password': password,
      };
  
      final response = await http.post('apiurl', body: params);
  
      if (response.statusCode != 200)
         throw Exception(response.body);
  
      return response.body;
  }
  </code></pre>
  
  <p>I'm adding the code from where the call to <em>login</em> happens. There is a TODO in the <em>_loginController</em> where I want to Show an Alert Dialog which shows the error.</p>
  
  <pre><code>class LoginWidgetState extends State&lt;LoginWidget&gt; {
    var _usernameController = new TextEditingController();
  
    var _passwordController = new TextEditingController();
  
    void _loginButtonClickHandler() {
      var username = _usernameController.text;
      var password = _passwordController.text;
  
      login(username, password).then((response) {
  
      }).catchError((e) {
        //TODO : show an Alert Here
      });
    }
  
    @override
    Widget build(BuildContext context) {
      var container = Center(
        child: Container(
      child: Column(
        children: &lt;Widget&gt;[
          TextField(
            decoration: InputDecoration(
              hintText: "username",
            ),
            controller: _usernameController,
          ),
          TextField(
            obscureText: true,
            decoration: InputDecoration(hintText: "password"),
            controller: _passwordController,
          ),
          RawMaterialButton(
            onPressed: _loginButtonClickHandler,
            child: Text("Login"),
          )
         ],
       ),
      ),
     );
  
     return container;
    }
  
  }
  </code></pre>