setText method
void
setText()
Sets the initial text of the text field, converting backend strings to taggables.
The backendToTaggable
function is used to convert backend strings to taggables.
It has the 'FutureOr' signature to allow for asynchronous operations.
Implementation
void setText(
String backendText,
FutureOr<T?> Function(String prefix, String backendString)
backendToTaggable,
) async {
final StringBuffer tmpText = StringBuffer();
int position = 0;
for (final Match match in _getTagMatches(backendText)) {
final textBeforeMatch = backendText.substring(position, match.start);
tmpText.write(textBeforeMatch);
position = match.end;
final tagStyle = tagStyles
.where((style) => match.group(0)!.startsWith(style.prefix))
.firstOrNull;
if (tagStyle == null) {
tmpText.write(match.group(0));
continue;
}
final taggable = await backendToTaggable(
tagStyle.prefix, match.group(0)!.substring(tagStyle.prefix.length));
if (taggable == null) {
tmpText.write(match.group(0));
continue;
}
final tag = Tag<T>(taggable: taggable, style: tagStyle);
final tagText = tag.toModifiedString(
toFrontendConverter,
toBackendConverter,
isFrontend: false,
);
_tagBackendFormatsToTaggables[tagText] = taggable;
tmpText.write(tagText);
}
final textAfterAllTags =
backendText.substring(position, backendText.length);
tmpText.write(textAfterAllTags);
text = tmpText.toString().trimRight();
}