searchUrlInText function

List<String> searchUrlInText(
  1. String text
)

Implementation

List<String> searchUrlInText(String text) {
  List<String> urls = [];
  bool scanCompleted = false;
  while (!scanCompleted) {
    if (text.contains('https://') || (text.contains('http://'))) {
      var index = text.length;
      if (text.contains('https://')) {
        index = text.indexOf('https://');
      } else {
        index = text.indexOf('http://');
      }
      var linkFinalIndex = text.indexOf(' ', index);
      if (linkFinalIndex == -1) {
        linkFinalIndex = text.indexOf('\n', index);
        if(linkFinalIndex == -1) {
          linkFinalIndex = (text.length);
        }
      }
      final link = text.substring(index, linkFinalIndex);
      urls.add(link);
      if (linkFinalIndex == (text.length - 1)) {
        scanCompleted = true;
        break;
      }
      text = text.substring(linkFinalIndex);
    } else {
      scanCompleted = true;
    }
  }
  return urls;
}