title: Building widget in Flutter when response statusCode on API call is >400
body: <p>So I'm trying to call the REST API for the login here. This is in my api_services.dart where I am calling all the APIs for the application. </p>
  
  <p><code>api_services.dart</code> </p>
  
  <pre><code>Future&lt;User&gt; loginUser(String email, String password) 
  async {
      final response = await http.post(serverOauthUrl+'/token',
      headers: {
      HttpHeaders.AUTHORIZATION: "xxxx"
    },
    body: {
       "email":"$email",
       "password":"$password",
    }
  );
  print(response.statusCode);
  final responseJson = json.decode(response.body);
  return new User.fromJson(responseJson);
  }
  </code></pre>
  
  <p>And there are two ways I can call this <code>loginUser()</code> method in my UI files and get the response. One that uses the then() method and the other uses FutureBuilder. However, in none of the method, can I get the status code. My use case is that when the status code is >400, I will build a widget that shows the error message.</p>
  
  <p><code>login_screen.dart</code> </p>
  
  <p>then() method code:</p>
  
  <pre><code>_callLoginAPI(String email, String password){
    loginUser(userName, password, "password").then((response) {
          response.data.token;
         // want my status code here as well along with response data
      }
      else
      {
        //todo show something on error
      }
    }, onError: (error) {
      debugPrint(error.toString());
    });
  
  
  }
  </code></pre>
  
  <p>Or using FutureBuilder :</p>
  
  <pre><code>return new FutureBuilder&lt;User&gt;(
  
        future: loginUser(email, password),
        builder: (context, snapshot) {
  
          if (snapshot.hasData) {
            print(snapshot.data.token);
          } else if (snapshot.hasError) {
            print(snapshot.error);
            return new Text("${snapshot.error}");
          }
          return new CircularProgressIndicator();
        },
      );
  </code></pre>
  
  <p>What I want to do is something like this  </p>
  
  <pre><code>if(response.statusCode &gt; 400)
     return new Text("Error"):&lt;/code&gt;
  </code></pre>