21

I'm modelling some JSON - and using the following lines

data class Metadata(
        val id: String,
        val creators: Array<CreatorsModel>
)

along with:

data class CreatorsModel (
        val role: String,
        val name: String
)

However keep seeing the error: Array property in data class error.

Any ideas why this is?

FYI, the JSON looks like:

{
"id": "123",
"creators": [{
   "role": "Author",
    "name": "Marie"
    }
  ]
}
1
  • 1
    maybe you should try List instead of Array? Commented Jul 29, 2019 at 7:55

1 Answer 1

37

In Kotlin you should aim to use List instead of Array where possible. Array has some JVM implications, and although the compiler will let you, the IDE may prompt you to override equals and hashcode manually. Using List will make things much simpler.

You can find out more about the difference here: Difference between List and Array types in Kotlin

Sign up to request clarification or add additional context in comments.

2 Comments

what about ByteArray? I plan to use Entity with image field to save it for the Room
@user924 Your best bet is to wrap the ByteArray in a wrapper and use that wrapper in the data class. That will overcome this limitation.

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.