Dasun Dola
Dasun Dola

Reputation: 623

Error " The getter 'userID' isn't defined for the type 'Response'. "in flutter

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

Upvotes: 0

Views: 58

Answers (2)

Azhar Husain Raeisi
Azhar Husain Raeisi

Reputation: 197

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);

Upvotes: 1

My Car
My Car

Reputation: 4576

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"]);
}

Upvotes: 1

Related Questions