Step #2: Switching to a data Class

A typical pattern for model objects in Kotlin is for them to be data classes. data classes with val properties are immutable: you do not change a model, but instead replace it with a new instance that has the new values.

You can learn more about data classes in the "Data Class" chapter of Elements of Kotlin!

So, add the data keyword before class, giving you:

package com.commonsware.todo

data class ToDoModel {
}

This will immediately show a red undersquiggle, indicating that Android Studio is unhappy about something:

Android Studio, Yelling
Android Studio, Yelling

That is because a data class must have a constructor with 1+ parameters. We will add that constructor in the next section.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.