I have a use case where depending on the presence or absence of a property value an object referencing it is either created or removed. I am using a Loader
for the purpose, where the active
property is bound to the property, that is when the property has a non-null value the loader is activated, when it is set to null it is deactivated.
However the problem is that the loader doesn't release its item immediately, so for a moment the item references a null property, thus is unable to access data, while the setting of the property to null triggers reevaluations that result in a swarm of cannot read property x of null
.
Simple logic suggest this should not happen. So I thought that maybe the problem is the order of binding evaluations is wrong, resulting in the item's bindings being evaluated before the loader is deactivated. So I tried removing the binding for active
and setting it up manually. The problem however persisted.
So here is a minimal representation to illustrate what's going on:
Window {
id: main
visible: true
width: 500
height: 300
property QtObject object : QtObject {
property QtObject subObject: null
}
QtObject {
id: subo
property int i : 1
}
Loader {
id: ld
active: false
sourceComponent: Text {
text: object.subObject.i
font.pointSize: 20
}
}
MouseArea {
anchors.fill: parent
onClicked: {
if (object.subObject) {
ld.active = false
object.subObject = null
} else {
object.subObject = subo
ld.active = true
}
}
}
}
Note that in this case the loader is explicitly deactivated before the property is set to null
, yet nonetheless, every time that this happens I get a type error in the console:
qrc:/main.qml:25: TypeError: Cannot read property 'i' of null
This doesn't seem like correct behavior. So maybe a bug? Or am I missing something? Any suggestions on how to work around that limitation? Note that this is somehow avoided when using views or repeaters.
Update: To clarify further, in my actual production code the loader's item cannot really exist without referencing that property. So the idea was that the object should only be created when the property has a value other than null
and be destroyed when it is null
.