Why this json response is a List and not a Map ?

220 views
Skip to first unread message

Pascal Fournier

unread,
Jun 12, 2019, 10:01:36 AM6/12/19
to Flutter Development (flutter-dev)
Hello to all,

i can't understand why my response.body from a http.get(url) return a List and not a Map....

I call my backend with http.get(url) then my php code return this : 


php code ..........
$arr_final = array();
$arr = array ();

// Query my database
while($row=$sql_res->fetch_assoc())
        {            
            $arr['city'] = $row["city"]; 
            $arr['country'] = $row["country"];
            array_push($arr_final,$arr);  
        }
echo json_encode($arr_final);


To get the response in Flutter i did :

List<dynamic> list=[];
if (response.statusCode == 200) {
print(json.decode(response.body));
list= json.decode(response.body);
}


This way... it works.

I thought that json return Map....
here is a print of my response.body :

[{city: Montréal, country: Canada}, {city: Paris, country: France}]



So why it is a List and not a List of Maps ??? because when it'S in curly braces {} it's a map... no?

thanks a lot !

Pascal

Ralph Bergmann

unread,
Jun 12, 2019, 10:16:06 AM6/12/19
to flutt...@googlegroups.com
I'm not a PHP developer but echo json_encode($arr_final) sounds like it
converts an Array (aka List) to a JSON string and converting an Array
means you get an Array. IF you want to get a Map you need to create and
convert a Map on PHP side too.
Ralph


Am 12.06.19 um 16:01 schrieb Pascal Fournier:
> Hello to all,
>
> i can't understand why my response.body from a http.get(url) return a
> List and not a Map....
>
> I call my backend with http.get(url) then my php code return this :
>
>
> php code ..........
> $arr_final = array();
> $arr = array ();
>
> // Query my database
> while($row=$sql_res->fetch_assoc())
>         {
> $arr['city'] = $row["city"];
> $arr['country'] = $row["country"];
> array_push($arr_final,$arr);
>         }
> echo json_encode($arr_final);
>
>
> To get the response in Flutter i did :
>
> List<dynamic> list=[];
>
> if (response.statusCode ==200) {
> print(json.decode(response.body));
> list= json.decode(response.body);
> }
>
>
>
> This way... it works.
>
> I thought that json return Map....
> here is a print of my response.body :
>
> *
> |
> [{city:Montréal,country:Canada},{city:Paris,country:France}]
> |
>
> *
>
> So why it is a List and not a List of Maps ??? because when it'S in
> curly braces {} it's a map... no?
>
> thanks a lot !
>
> Pascal
>
> --
> You received this message because you are subscribed to the Google
> Groups "Flutter Development (flutter-dev)" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to flutter-dev...@googlegroups.com
> <mailto:flutter-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/flutter-dev/b5216e94-3936-45fe-ba1f-f9eb2268c31b%40googlegroups.com
> <https://groups.google.com/d/msgid/flutter-dev/b5216e94-3936-45fe-ba1f-f9eb2268c31b%40googlegroups.com?utm_medium=email&utm_source=footer>.

Ralph Bergmann

unread,
Jun 12, 2019, 10:26:39 AM6/12/19
to flutt...@googlegroups.com
In this case you get a List of Maps but the Dart compiler doesn't knwo it.

The best solution should be like this:

if (!(response.body is List<dynamic>)) {
throw UnexpectedResponseException();
}

final List<dynamic> list = response.body as List<dynamic>;
for (var item in list) {
if (!(item is Map<String, dynamic>)) {
throw UnexpectedResponseException();
}

print('item: $item');
}


Am 12.06.19 um 16:01 schrieb Pascal Fournier:

Pascal Fournier

unread,
Jun 12, 2019, 10:44:47 AM6/12/19
to Flutter Development (flutter-dev)
Hello Ralph !

Thanks for your answer.
I really appreciate!

I try this :

final List<dynamic> list = response.body as List<dynamic>;
for (var item in list) {
   
if (!(item is Map<String, dynamic>)) {
     
throw UnexpectedResponseException();
   
}

   
print('item: $item');

}


And it works perfectly.

I will check for my PHP code. But the most important, is that everything is working :)

Thanks a lot and have a great day ! 

Pascal

nony bright

unread,
Jun 14, 2019, 12:00:11 AM6/14/19
to Flutter Development (flutter-dev)
The response looks correct. Your PHP code is returning an array of associative arrays and your http response is a List of maps. Did you just want a single Map?

Pascal Fournier

unread,
Jun 14, 2019, 8:57:25 AM6/14/19
to Flutter Development (flutter-dev)
Hello !

Thanks for your message ! This way is OK for me, but in the past, my PHP response was not an array, so i used a Map.. but the way i did now (returning an array) i thought that i can only use a map....
But that way is ok for me !!!

unless the way I do is not the right way
Thanks a lot ! 

Pascal
Reply all
Reply to author
Forward
0 new messages