1

I have a very easy question dont know why I am not able to find it anywhere I just need to print the specific value of data

My code

  Future<http.Response> _trySubmit() async {
    final isValid = _formKey.currentState.validate();
    FocusScope.of(context).unfocus();

    if (isValid) {
      _formKey.currentState.save();

      print(_userEmail.trim());
      print(_userPassword.trim());

      var map = new Map<String, dynamic>();


      map['grant_type'] = 'password';
      map['username'] = _userEmail.trim();
      map['password'] = _userPassword.trim();


      http.Response res = await http.post(
        'http://sublimeapi.netcodesolution.com/token',
        headers: <String, String>{
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: map,
      );
      var data = res.body;
      print(data);
    }
    }

Its printing the value like this

I/flutter ( 5147):{"access_token":"FwYttAQIDDSRpuFFUgzznmMYgMNNfiW4OvQ4","token_type":"bearer","expires_in":86399}

I need to print just access_token value

Something like this print(data.access_token)

3 Answers 3

2

Here data is a Map. So if you want to print a specific value out of it, you need to mention the corresponding key name like this

print(data['access_token']);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks its showing this error on access_token The argument type 'String' can't be assigned to the parameter type 'int'
On which line of code? It must be when you are trying to pass this access_token value to an int parameter.
its showing on access_token when i just print the value as you say like this print(data['access_token']); its showing red line on access_token
It should not show this error on that line. Are you sure it is showing the error there?
0

You need to decode the response result before printing the value :

 http.Response res = await http.post(
            'http://sublimeapi.netcodesolution.com/token',
            headers: <String, String>{
              'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: map,
          );
          var data = json.decode(res.body.toString());
          print(data["access_token"]);

Don't forget importing :

import 'dart:convert';

Comments

0

Your can try something like this: Decode the json response first then access the data in the Map

http.Response res = await http.post(
    'http://sublimeapi.netcodesolution.com/token',
    headers: <String, String>{
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: map,
  );
     var responseBody = json.decode(res.body);
     print(responseBody['access_token']); //This should return your token
 }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.