skip to Main Content

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

2

Answers


  1. 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"]);
    }
    
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search