Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot be @Required or @NotNull

When using version 4.3.3 of REALM for Android development I get the following error:

Error: Field "groupName" with type "pizware.evaluapp.Models.Group" can not be @Required or @NotNull.

but I do not use any of those labels for any field. Does anyone know what is going on?

like image 415
Erick piz Avatar asked Feb 01 '18 03:02

Erick piz


1 Answers

When you use Kotlin, then Realm checks against nullability on your field.

For example,

class Blah: RealmObject() {
    var group: Group? = null
}

Then group doesn't get implicit @Required annotation.

But if you do

class Blah: RealmObject() {
    var group: Group = Group()
}

This won't work, because Realm cannot guarantee non-nullability for a singular link. So it'll throw the error you're getting.

If you want to ignore this because for example you are making a backing field that avoids null value return

get() = group ?: Group()

Then you can use (since 4.1.0):

kapt {
  arguments {
    arg("realm.ignoreKotlinNullability", true)
  }
}

In which case Realm won't try to handle your nullability implicitly (and map the Kotlin nullability to the field's @Required). But you should use this only if you actually know what you're doing.

like image 94
EpicPandaForce Avatar answered Sep 28 '22 03:09

EpicPandaForce