prepareForEditingMode method
In edit mode of text field with mentions, it is necessary to call this
method during initialization, where to pass a list of previously set
mentions. This will correctly create the set of mentioned users
_mentionedObject
and the list of current mentions mentions
.
Implementation
void prepareForEditingMode(List<MentionModel> mentions) {
/// Prepare set of mentioned users
_addMentionedObjectsFromMentionsList(mentions);
/// Prepare list of current mentions
this.mentions = mentions.map((mention) {
/// Mentions' tags is not stored on backend usually, only positions and id.
/// So we need to add tags to each mention.
/// We can get them from the set of mentioned objects that we formed earlier
/// from the text (by position of mentions)
final mentionName = _mentionedObject
.firstOrNullWhere((object) => object.id == mention.id)
?.mentionName;
return MentionModel(
id: mention.id,
mentionName: mentionName != null ? '$tag$mentionName' : '',
locationStart: mention.locationStart,
locationEnd: mention.locationEnd,
tagType: tag,
);
}).toList();
}