I have the following data class:
data class TimesheetEntry(
val id: Int = 0,
val title: String = "",
val category: String = "",
var entryDate: LocalDate,
var startTime: LocalTime,
var endTime: LocalTime,
val imageBitmap: Bitmap? = null
)
I am trying to obtain timeSheetEntry data from Firestore however the app keeps crashing whenever I open the activity to view the entries.
When I check the logcat it shows me the following error:
java.lang.RuntimeException: Could not deserialize object. Class java.time.LocalDate does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped (found in field 'entryDate')
I tried assigning the system date to it however that did not work
data class TimesheetEntry(
val id: Int = 0,
val title: String = "",
val category: String = "",
val entryDate: LocalDate = LocalDate.now(),
val startTime: LocalTime= LocalTime.now(),
val endTime: LocalTime= LocalTime.now(),
val imageBitmap: Bitmap? = null
)
I also tried hard coding dates and times and it made no difference.
data class TimesheetEntry(
val id: Int = 0,
val title: String = "",
val category: String = "",
val entryDate: LocalDate = LocalDate.of(2023, 6, 9),
val startTime: LocalTime = LocalTime.of(9, 0),
val endTime: LocalTime = LocalTime.of(17, 30),
val imageBitmap: Bitmap? = null
)
2
Answers
Data classes in Kotlin must have at least one constructor parameter. That is necessary because data classes automatically generate
equals
andhashCode
methods from the constructor parameters. So there must be at least one.You can always supply default values for your parameters: This way you can create an object without supplying any parameters which looks like it used a no-args constructor.
You can also make your data class a regular class, then there are no more restrictions on having a no-args constructor.
For the other question concerning how to retrieve data from Firestore, I suggest you ask a new question with much more detail.
While @Leviathan is true when referring to data classes in Kotlin, please notice that you’re getting the following error:
Not because the
TimesheetEntry
class does not define a no-argument constructor but because the LocalDate does not define a no-argument constructor. So this error is thrown because you have added in your class fields that are not supported data types. So the best option that you have is to use to change theentryDate
,startTime
, andendTime
to Date and use the ServerTimestamp annotation as explained in my answer from the following post:P.S. Also don’t the
imageBitmap
is not a supported data type. Try to save the image in Cloud Storage of Firebase not in Firestore.