subscribeToComponentEvent function Null safety
Subscribes to events emitted by the components
eventName
is Flutter event name (tap, tapUp, tapDown ...)componentName
is Figma element nameindex
is index of componentName in case there are multiple instances with the same name, defaults to 0 (first found element with componentName) Example:
Event myEvent = subscribeToComponentEvent('tap', 'MyButton', (e){
print('Target: ${e.target}, Payload: ${e.payload}');
})
Implementation
Event subscribeToComponentEvent(
String eventName, String componentName, Function(Event) callback,
[int index = 0]) {
var evt = Event(eventName, callback, componentName, index);
if (!eventSubscriptions.containsKey(eventName)) eventSubscriptions[eventName] = Map();
bool? hasComponent = eventSubscriptions[eventName]?.containsKey(componentName);
hasComponent ??= false;
if (!hasComponent) eventSubscriptions[eventName]?[componentName] = Map();
bool? hasEvent = eventSubscriptions[eventName]?[componentName]?.containsKey(index);
hasEvent ??= false;
if (!hasEvent) eventSubscriptions[eventName]?[componentName]?[index] = List<Event>.empty(growable: true);
eventSubscriptions[eventName]?[componentName]?[index]?.add(evt);
int? length = eventSubscriptions[eventName]?[componentName]?[index]?.length;
length ??= 0;
evt.id = length - 1;
return evt;
}