fetchPreview method

Future<ChatUIKitPreviewObj?> fetchPreview(
  1. String url, {
  2. String? messageId,
})

Implementation

Future<ChatUIKitPreviewObj?> fetchPreview(String url,
    {String? messageId}) async {
  try {
    final response = await fetchWithRedirects(url);
    final document = parse(await response.transform(utf8.decoder).join());
    if (messageId != null) {
      _fetchingIds.add(messageId);
    }
    String? title = document.head?.querySelector('title')?.text;
    String? desc = document.head
        ?.querySelector("meta[name='description']")
        ?.attributes['content'];
    String? image = document.body?.querySelector('img')?.attributes['src'];

    return title?.isNotEmpty == true
        ? ChatUIKitPreviewObj(
            title: title,
            description: desc ?? '',
            imageUrl: image ?? '',
            url: url,
          )
        : null;
  } catch (e) {
    return null;
  }
}