Home > Back-end >  Error " The getter 'userID' isn't defined for the type 'Response'. &qu
Error " The getter 'userID' isn't defined for the type 'Response'. &qu

Time:12-22

In my code has a textfield in there can type name then click button that should pass to http URL and then should get "userID" from that URL.

button code

 Future<void> onJoin() async {
    setState(() {
      myController.text.isEmpty
          ? _validateError = true
          : _validateError = false;
    });



    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => loadData(myController.text)),
    );
  }

get "userID" from http url

  loadData(String myController) async {
    var userID = "";
    try {
      final uri =
          Uri.parse('http://github.users.com/$myController');
      final response = await http.get(uri);

      if (response.userID != null) {
        print("userID : "   userID);
      }
    } catch (e) {
      print("Invalid name entered ");
    }
  }

If successfully retrieve userId then that should print if it null then should display "Invalid name entered ".

Error

enter image description here

CodePudding user response:

Try the following code:

final response = await http.get(uri);

final Map<String, dynamic> data = response.body;

if (data["userID"] != null) {
  print("userID : "   data["userID"]);
}

CodePudding user response:

response.body is an encoded json thats why you are getting

 A value of type 'String' can't be assigned to a variable of type 'Map<String, dynamic>

so decode your response body, as below

final Map<String, dynamic> data = jsonDecode(response.body);
  • Related