title: post http request : The getter 'length' was called on null flutter
body: <p>I want to create a user when I call my API.
  I created a Future with the URL and I call my model in the screen page.
  I get all data in my form but when I call my API, I have this error :</p>
  
  <p>The getter 'length' was called on null...</p>
  
  <p>User model :</p>
  
  <pre><code>class Candidate {
    int id;
    String firstname;
    String lastname;
    String email;
  
    Candidate({this.id, this.firstname, this.lastname, this.email});
  
    factory Candidate.fromJson(Map&lt;String, dynamic&gt; json) {
      return Candidate(
        id: json['id'],
        firstname: json['firstname'],
        lastname: json['lastname'],
        email: json['email'],
      );
    }
  
    Map toMap() {
      var map = new Map&lt;String, dynamic&gt;();
      map["id"] = id;
      map["firstname"] = firstname;
      map["lastname"] = lastname;
      map["email"] = email;
      return map;
    }
  
    Future&lt;Candidate&gt; candidateAuth({Map body}) async {
      String url = 'http://10.0.2.2:3000/v1/api/auth/candidate';
      final response = await http.post(url, body: body, headers: {"Accept": "application/json"});
  
      if (response.statusCode == 201) {
        return Candidate.fromJson(json.decode(response.body));
      } else {
        throw Exception('Failed auth');
      }
    }
  }
  </code></pre>
  
  <p>EDIT :</p>
  
  <p>I added all code of login page with the modification. </p>
  
  <p>In the login page  : </p>
  
  <pre><code>import 'package:blackbox/models/candidate_model.dart';
  import 'package:blackbox/screens/theme_page.dart' as t;
  import 'package:flutter/material.dart';
  
  class Login extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() {
      return new _Login();
    }
  }
  
  class _Login extends State&lt;Login&gt; {
    final _formKey = GlobalKey&lt;FormState&gt;();
    String email, lastname, firstname;
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        appBar: new AppBar(
          leading: Image.asset(
            'assets/img/logo_ineat.png',
            fit: BoxFit.contain,
            height: 32,
          ),
          title: Text('BlackBox'),
        ),
        body: new Center(
          child: new SingleChildScrollView(
            child: new Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: &lt;Widget&gt;[
                new Container(
                  margin: EdgeInsets.only(top: 10.0, bottom: 20.0),
                  alignment: Alignment.topCenter,
                  child: new Text('Se Connecter',
                      style: new TextStyle(
                          fontSize: 24, fontWeight: FontWeight.bold)),
                ),
                new Card(
                  elevation: 10,
                  color: Colors.pink,
                  child: new Container(
                    width: MediaQuery.of(context).size.width / 2,
                    height: MediaQuery.of(context).size.height / 3,
                    child: new Image.asset('assets/img/logo_ineat.png',
                        fit: BoxFit.contain),
                  ),
                ),
                new Form(
                  key: _formKey,
                  child: new Column(
                    children: &lt;Widget&gt;[
                      new Row(
                        children: &lt;Widget&gt;[
                          new Container(
                            width: MediaQuery.of(context).size.width / 5,
                            margin:
                                EdgeInsets.only(left: 10, top: 5, right: 10.0),
                            child: new Text("Nom : "),
                          ),
                          new Container(
                            width: MediaQuery.of(context).size.width / 1.4,
                            margin: EdgeInsets.only(top: 5, right: 10.0),
                            child: new TextFormField(
                              keyboardType: TextInputType.text,
                              decoration: new InputDecoration(
                                  labelText: 'Entrez votre nom'),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'Veuillez remplir le champ nom';
                                }
                                lastname = value;
                                return null;
                              },
                            ),
                          ),
                        ],
                      ),
                      new Row(
                        children: &lt;Widget&gt;[
                          new Container(
                            width: MediaQuery.of(context).size.width / 5,
                            margin:
                                EdgeInsets.only(left: 10, top: 5, right: 10.0),
                            child: new Text("Prénom : "),
                          ),
                          new Container(
                            width: MediaQuery.of(context).size.width / 1.4,
                            margin: EdgeInsets.only(top: 5, right: 10.0),
                            child: new TextFormField(
                              keyboardType: TextInputType.text,
                              decoration: new InputDecoration(
                                  labelText: 'Entrez votre prénom'),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'Veuillez remplir le champ prénom';
                                }
                                firstname = value;
                                return null;
                              },
                            ),
                          ),
                        ],
                      ),
                      new Row(
                        children: &lt;Widget&gt;[
                          new Container(
                            width: MediaQuery.of(context).size.width / 5,
                            margin:
                                EdgeInsets.only(left: 10, top: 5, right: 10.0),
                            child: new Text("Email : "),
                          ),
                          new Container(
                            width: MediaQuery.of(context).size.width / 1.4,
                            margin: EdgeInsets.only(top: 5, right: 10.0),
                            child: new TextFormField(
                              keyboardType: TextInputType.emailAddress,
                              decoration: new InputDecoration(
                                  labelText: 'Entrez votre email'),
                              validator: (value) {
                                if (value.isEmpty) {
                                  return 'Veuillez remplir le champ email';
                                }
                                email = value.toLowerCase();
                                return null;
                              },
                            ),
                          ),
                        ],
                      ),
                      new Container(
                        margin: EdgeInsets.only(top: 30, bottom: 5, right: 10.0),
                        alignment: Alignment.bottomRight,
                        child: new RaisedButton.icon(
                            onPressed: () {
                              setState(() async {
                                if (_formKey.currentState.validate()) {
                                  Candidate newPost = new Candidate(
                                    lastname: lastname,
                                    firstname: firstname,
                                    email: email,
                                  );
                                  var candidate = await Candidate()
                                      .candidateAuth(body: newPost.toMap());
                                }
                              });
                            },
                            icon: Icon(Icons.check),
                            label: Text('Valider')),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }
  }
  
  </code></pre>
  
  <p>Error message:</p>
  
  <blockquote>
    <p>Exception has occurred.<br>
    NoSuchMethodError (NoSuchMethodError: The getter 'length' was called on null.
    Receiver: null Tried calling:
    length)  The following assertion was thrown while handling a gesture: 
    setState() callback argument returned a Future.  The setState() method
    on _Login#e2125 was called with a closure or method that returned a Future. 
    Maybe it is marked as "async". Instead of performing
    asynchronous work inside a call to setState(), first execute the work
    (without updating the widget state), and then synchronously update the
    state inside a call to setState()</p>
  </blockquote>