Jason O.
Jason O.

Reputation: 3300

Converting http response to a Flutter list

I'm having trouble converting HTTP response body to a Flutter list. In a debugger, the output of jsonDecode(response.body)['data']['logsread'] looks exactly like

[
   {
      "id": "9fd66092-1f7c-4e60-ab8f-5cf7e7a2dd3b",
      "email": "[email protected]"
   }
]

Yet, this returns false.

print((jsonDecode(response.body)['data']['logsread']) ==
[{
      "id": "9fd66092-1f7c-4e60-ab8f-5cf7e7a2dd3b",
      "email": "[email protected]"
}]);  // This returns false.

FYI. response.body =>

"{"data":{"logsread":[{"id":"9fd66092-1f7c-4e60-ab8f-5cf7e7a2dd3b","email":"[email protected]"}]}}"

Upvotes: 5

Views: 8844

Answers (4)

Xuzan
Xuzan

Reputation: 335

JsonDecode returns List<dynamic> but your another list is of type List<Map<String,String>>. So convert it to same type of list by creating to any Model and overriding == and hashcode.

and to compare two list you need ListEquality function. example :

    Function eq = const ListEquality().equals;
    print(eq(list1,list2));

I tried your code and done my way, check if this okay.

Model class:

    class Model {
        String id;
        String email;

        Model({
          this.id,
          this.email,
        });

        factory Model.fromJson(Map<String, dynamic> json) => new Model(
              id: json["id"],
              email: json["email"],
            );

        Map<String, dynamic> toJson() => {
              "id": id,
              "email": email,
            };


        @override
        bool operator ==(Object other) =>
            identical(this, other) ||
                other is Model &&
                    runtimeType == other.runtimeType &&
                    id == other.id &&
                    email == other.email;

        @override
        int get hashCode =>
            id.hashCode ^
            email.hashCode;

      }

main.dart

    import 'package:collection/collection.dart';

    var body =
            '{"data":{"logsread":[{"id":"9fd66092-1f7c-4e60-ab8f-5cf7e7a2dd3b","email":"[email protected]"}]}}';
        var test1 = (jsonDecode(body)['data']['logsread'] as List)
            .map((value) => Model.fromJson(value))
            .toList();
        var test2 = ([
          {"id": "9fd66092-1f7c-4e60-ab8f-5cf7e7a2dd3b", "email": "[email protected]"}
        ]).map((value)=>Model.fromJson(value)).toList();

        Function eq = const ListEquality().equals;
        print(eq(test1,test2));

I hope this is what you are looking for.

Upvotes: 4

magicleon94
magicleon94

Reputation: 5162

Your parse is correct, but I think that's not the right way to compare list of maps. I think that doing == you're simply comparing the objects and not their content.

I suggest you to check here for map compare and here for list compare.

This way you would be checking the contents, and not roughly the objects (which are two different instances, so yeah technically they're different even if they have the same contents).

Upvotes: 1

Amit Jangid
Amit Jangid

Reputation: 2889

You should also provide position of the object you want to retrieve id from. Did you try doing this?

Example:

var id = ['data']['logsread'][0]['id'];
var email= ['data']['logsread'][0]['email'];

I mostly do this.

Upvotes: 2

kishansinh
kishansinh

Reputation: 101

Try to first convert into the map and then use

import 'dart:convert';

//Decode response string to map
Map<String, dynamic> map = json.decode("jsonString");

xyz = map['data']['logsread'];

Upvotes: 0

Related Questions