dataBind method
Binds a ViewModelInstance to the StateMachine.
- The
dataBindparameter specifies which view model instance to bind to the StateMachine.
Returns the ViewModelInstance that was bound to the StateMachine.
Throws a RiveDataBindException if the ViewModelInstance could not be found.
Implementation
ViewModelInstance dataBind(DataBind dataBind) {
final vmi = switch (dataBind) {
AutoBind() => file.createDefaultViewModelInstance(artboard),
BindByInstance() => dataBind.viewModelInstance,
BindByIndex() => _safeViewModel?.createInstanceByIndex(dataBind.index),
BindByName() => _safeViewModel?.createInstanceByName(dataBind.name),
BindEmpty() => _safeViewModel?.createInstance(),
};
if (vmi == null) {
final message = switch (dataBind) {
AutoBind() =>
'Default view model instance not found. Make sure the instance is set to exported in the Rive Editor.',
BindByInstance() => 'Failed to use the provided view model instance.',
BindByIndex() =>
'View model instance by index `${dataBind.index}` not found.',
BindByName() =>
'View model instance by name `${dataBind.name}` not found.',
BindEmpty() =>
'Something went wrong. Could not create a view model instance.',
};
throw RiveDataBindException(message);
}
stateMachine.bindViewModelInstance(vmi);
return vmi;
}