Flutter Web: http Response Status Code?

279 views
Skip to first unread message

Kevin Roberson

unread,
Jan 27, 2021, 4:55:19 PM1/27/21
to Flutter Development (flutter-dev)
Hello,
I'm posting to an API server and trying to check the response status code to determine success or failure.  According to several examples I've found, there should be a Response.statusCode integer value that is returned.  However, it doesn't show in code complete on VS Code.  Instead, editor gives me the following:
The getter 'statusCode' isn't defined for the type 'Future<Response>'.
Try importing the library that defines 'statusCode', correcting the name to the name of an existing getter, or defining a getter or field named 'statusCode'.dart(undefined_getter)  

What am I missing?

Thanks,
Kevin

import 'package:http/http.dart' as http;

onPressed: () {
                      if (_registerFormKey.currentState.validate()) {
                        Future<http.Response> addResponse = addUser();
                        if (addResponse.statusCode == 200) {
                          Navigator.of(context).pushNamed(Success.route);
                        } else {
                          errorMsg(context);
                        }
                      }
                    },

Future<http.Response> addUser() {
    var regForm = new Map();

    regForm['firstName'] = _firstNameTextController.text;
    regForm['lastName'] = _lastNameTextController.text;
    regForm['email'] = _emailTextController.text;
    regForm['relationship'] = _dropdownValue;
    regForm['relationOther'] = _relationTextController.text;
    regForm['username'] = _usernameTextController.text;
    regForm['password'] = encr(_passwordTextController.text);
    regForm['passwordHint'] = _passwordHintTextController.text;
    regForm['passwordQ1'] = _passwordRecoveryQ1TextController.text;
    regForm['passwordA1'] = _passwordRecoveryA1TextController.text;
    regForm['passwordQ2'] = _passwordRecoveryQ2TextController.text;
    regForm['passwordA2'] = _passwordRecoveryA2TextController.text;
    regForm['passwordQ3'] = _passwordRecoveryQ3TextController.text;
    regForm['passwordA3'] = _passwordRecoveryA3TextController.text;

    return http.post(
      Constants.API_URL + '/addUser',
      headers: <StringString>{'Content-Type': 'application/json'},
      body: json.encode(regForm),
    );
  }

Andy T Greenshaw

unread,
Jan 27, 2021, 5:01:18 PM1/27/21
to Kevin Roberson, Flutter Development (flutter-dev)
You should ‘await’ the http.Post and change return type of addUser to just http.Response.




----------
--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/1af02861-0006-49fe-a802-1ab349187390n%40googlegroups.com.

Kevin Roberson

unread,
Jan 27, 2021, 6:09:52 PM1/27/21
to Flutter Development (flutter-dev)
Thanks for the quick response Andy, but when I remove Future from the addUser function, I get:

Functions marked 'async' must have a return type assignable to 'Future'.
Try fixing the return type of the function, or removing the modifier 'async' from the function body.dart(illegal_async_return_type)  

Suzuki Tomohiro

unread,
Jan 27, 2021, 6:46:02 PM1/27/21
to Kevin Roberson, Flutter Development (flutter-dev)
The error message tells what to do. Which part di you need help?

Learn the difference between “ Future<Response>” and “Response”. 


Kevin Roberson

unread,
Jan 27, 2021, 9:09:19 PM1/27/21
to Flutter Development (flutter-dev)
Andy, here is the updated code, as you requested.  Thanks for your help.

import 'package:http/http.dart' as http;

onPressed: () {
    if (_registerFormKey.currentState.validate()) {
      `addResponse = addUser();
       if (addResponse.statusCode == 200) {
          Navigator.of(context).pushNamed(Success.route);
       } else {
           errorMsg(context);
       }
   }
},


http.Response addUser() async {
    http.Response res;
    var regForm = new Map();

    regForm['firstName'] = _firstNameTextController.text;
    regForm['lastName'] = _lastNameTextController.text;
    regForm['email'] = _emailTextController.text;
    regForm['relationship'] = _dropdownValue;
    regForm['relationOther'] = _relationTextController.text;
    regForm['username'] = _usernameTextController.text;
    regForm['password'] = encr(_passwordTextController.text);
    regForm['passwordHint'] = _passwordHintTextController.text;
    regForm['passwordQ1'] = _passwordRecoveryQ1TextController.text;
    regForm['passwordA1'] = _passwordRecoveryA1TextController.text;
    regForm['passwordQ2'] = _passwordRecoveryQ2TextController.text;
    regForm['passwordA2'] = _passwordRecoveryA2TextController.text;
    regForm['passwordQ3'] = _passwordRecoveryQ3TextController.text;
    regForm['passwordA3'] = _passwordRecoveryA3TextController.text;

    res = await http.post(
      Constants.API_URL + '/addUser',
      headers: <String, String>{'Content-Type': 'application/json'},
      body: json.encode(regForm),
    );

    return res;
  }

On Wednesday, January 27, 2021 at 4:01:18 PM UTC-6 andy...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages