subscribeToComponentEvent function Null safety

Event subscribeToComponentEvent(
  1. String eventName,
  2. String componentName,
  3. dynamic callback(
    1. Event
    ),
  4. [int index = 0]
)

Subscribes to events emitted by the components

  • eventName is Flutter event name (tap, tapUp, tapDown ...)
  • componentName is Figma element name
  • index 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;
}