I am trying to get the JSON response from the server and output it to the console.
Future login() async {
var response = await http.get(
Here are 2 common ways this could go wrong:
If your response is a json object like
[
{
key1: value1,
key2: value2,
key3: value3,
},
{
key1: value1,
key2: value2,
key3: value3,
},
.....
]
Then, we use data[0]["name"]
, not data[0].name
Unless we cast to an object that has the name property, we cannot use data[0].name
We cast like this data = json.decode(response.body).cast
ObjectName
can be whatever object you want (Inbuilt or Custom). But make sure it has the name property
If your response is a JSON object like
{
dataKey: [
{
key1: value1,
key2: value2,
key3: value3,
}
]
}
Then json.decode
will return a Map, not a List
Map map = json.decode(response.body);
List data = map["dataKey"];
print(data[0]["name"]);