title: Managing errors on post in Flutter
body: <p>I made an application for the company that i work, the app its working and solved some problems, but the code is a mess, since i'm not a official programmer and this is my very first application. Now i'm trying to improve the code.</p>
  
  <p>The problem here is that i dont know how to check if the http post was successful.</p>
  
  <p>In the code below you can see that i made some error treatment but its not working properly.
  For example, if the app doesnt receive the "ok" message from the server it will return a error, but if the internet isnt working it will not return the error because it will be trying forever to send the post.
  I would like to always check if the post was successfull and notify the user, or show a error after a time of trying (like 2 seconds, i dont know), whats the best way to solve this?</p>
  
  <p>Any other tips to improve the code are welcome.</p>
  
  <pre><code>    if ((_usuarioController.text.isEmpty) ||
          (_placaController.text.isEmpty) ||
          (_boxController.text.isEmpty) ||
          (dropdownValue1 == "Vehicle type")) {
        Toast.show(
          "\n  Complete all fields  \n",
          context,
          duration: Toast.LENGTH_LONG,
          gravity: Toast.CENTER,
          backgroundRadius: 5.0,
        );
      } else if (_pecasList.length &lt; 1) {
        showDialog(
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                  title: new Text("Empty List"),
                  actions: &lt;Widget&gt;[
                    new FlatButton(
                        child: new Text("Close"),
                        onPressed: () {
                          Navigator.of(context).pop();
                        }),
                  ]);
            });
      } else {
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: new Text("Send the items?"),
              actions: &lt;Widget&gt;[
                new FlatButton(
                    child: new Text("Close"),
                    onPressed: () {
                      Navigator.of(context).pop();
                    }),
                new FlatButton(
                    child: new Text("Send"),
  
                    onPressed: () async {
                      Map&lt;String, dynamic&gt; newDados = Map(); 
                      newDados["usuario"] = _usuarioController.text.trimLeft();
                      newDados["placa"] = _placaController.text.trimLeft();
                      newDados["box"] = _boxController.text.trimLeft();
                      newDados["tipo_veiculo"] = dropdownValue1;
                      _dadosList.add(newDados);
                      print(_pecasList + _dadosList);
  
                      Map&lt;String, String&gt; headers = new Map&lt;String, String&gt;();
                      headers["Content-type"] = "application/json";
                      headers["Accept"] = "application/json";
                      //String str = '{"take":55, "skip":"0"}';
                      final resp = await http.post('http://' + ipServidor,
                          body: jsonEncode(_dadosList +
                              _pecasList), //+ jsonEncode(_pecasList),
                          headers: headers);
  
                      print(resp.statusCode);
  
                      _dadosList
                          .clear(); //Cleans the list
                      print(resp.body);
                      if (resp.statusCode == 200) {
                        if (resp.body == "ok") {
                          setState(() {
                            print(_pecasList);
                            _pecasList.clear();
                            _placaController.clear();
                            _boxController.clear();
                            dropdownValue1 = "Vehicle type";
  
                            Navigator.of(context).pop();
                          });
                        } else {
                          showDialog(
                              context: context,
                              builder: (BuildContext context) {
                                return AlertDialog(
                                    title: new Text(
                                        "Error"),
                                    actions: &lt;Widget&gt;[
                                      new FlatButton(
                                          child: new Text("Close"),
                                          onPressed: () {
                                            Navigator.of(context).pop();
                                            Navigator.of(context).pop();
                                          }),
                                    ]);
                              });
                        }
                      } else {
                        print("communication error");
                        Navigator.of(context).pop();
                        showDialog(
                            context: context,
                            builder: (BuildContext context) {
                              return AlertDialog(
                                  title: new Text("communication error"),
                                  actions: &lt;Widget&gt;[
                                    new FlatButton(
                                        child: new Text("Close"),
                                        onPressed: () {
                                          Navigator.of(context).pop();
                                        }),
                                  ]);
                            });
                      }
                    })
              ],
            );
          },
        );
      }
    }```
  
  </code></pre>