How can I write into a Flag's custom field programmatically? I successfully managed to save a flag programmatically in Drupal 8 thanks to this help but I cannot manage to save data into a custom field I added to the flag entity.

The following code saves the flag but the field stay empty.

$entity = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($entity_id);
$flag_id = 'bookmark';
$flag_service = \Drupal::service('flag');
$flag = $flag_service-&gt;getFlagById($flag_id);

/* Here is where I'm trying to set my custom field value */
$flag-&gt;set('field_my_custom_field', $link_name);

$flag_service-&gt;flag($flag, $entity);


I cannot find any info into the documentation. Any help will be much appreciated.
I fix it with this code.
You should save the output of the function

$flag_service-&gt;flag($flag, $entity);   //Wrong method


in a variable like it

$id_flagged_entity = $flag_service-&gt;getFlagging($flag, $entity);


and set the title at this entity

$id_flagged_entity-&gt;set("field_my_custom_field", $link_name);
$id_flagged_entity-&gt;save();