1

I have a response that I got from my API request that I've decoded into a JSON object. When trying to convert this Json object into my class that contains a list of objects I run into the following error:

"Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>'".

I understand the meaning behind the error but cannot figure out a solution to it. Resources on this particular thing in Dart are fairly limited.

I've tried rewriting my fromJson method a few times, but keep running into the same error.

Below is my UserList class, the one I'm trying to convert the Json object into.

    class UserList {
      final List<UserObject> users;
      UserList({
        this.users,
      });

      addUser(UserObject user){
        users.add(user);
      }

      factory UserList.fromJson(List<dynamic> parsedJson) {
        List<UserObject> users = new List<UserObject>();
        users = parsedJson.map((i)=>UserObject.fromJson(i)).toList();

        return new UserList(
            users: users
        );
      }

      Map<String, dynamic> itemToJson() {
        return <String, dynamic>{
          'UserVal': this.users,
        };
      }

    }

And this is where I call the fromJson method:

    UserList list = UserList.fromJson(userVal);

userVal equals the following:

{UserVal: [{UID: test, Email: test@gmail.com, Phone: 1110001111, Name: Test1}, {UID: test, Email: test@gmail.com, Phone: 1110001111, Name: Test2}, {UID: test, Email: test@gmail.com, Phone: 1110001111, Name: Test3}, {UID: test, Email: test, Phone: 1110001111, Name: test4}]}

The output I'm looking for is to have a complete object that my method can then return and be used. However, it breaks on this method call.

Mariano Zorrilla
  • 7,165
  • 2
  • 34
  • 52
Jet.B.Pope
  • 642
  • 1
  • 5
  • 25

0 Answers0