getHeadlines method Null safety

Future<Map<String, List<Headline>>?> getHeadlines(
  1. int days
)

getHeadlines method gets all of the headlines for the given tickers returns null if any of the tickers are invalid

Implementation

Future<Map<String, List<Headline>>?> getHeadlines(int days) async {
  var now = DateTime.now();
  var earliest = now.subtract(new Duration(days: days));

  var ticks = tickers.split(" ");
  Map<String, List<Headline>> map = new Map();
  for (String tick in ticks) {
    //print(tick);
    List<Headline> list = [];
    var url = Uri.parse('https://finviz.com/quote.ashx?t=' + tick);
    var response = await http.get(url);
    //print('Response status: ${response.statusCode}');
    //print('Response body: ${response.body}');
    var doc = parse(response.body);
    var element = doc.getElementById("news-table");
    if (element == null) {
      continue;
    }
    //print(element!.outerHtml);
    var trs = element.getElementsByTagName("tr");
    var done = false;
    var dateprefix = '';
    var date = DateTime.now();
    for (Element ele in trs) {
      String headline = "";
      String link = "";
      String extract = "";
      for (Element td in ele.getElementsByTagName("td")) {
        //print(td.text);
        var tex = td.text;
        var sp = tex.split(' ');
        //print(sp.length);
        if (sp.length == 1) {
          date =
              DateFormat('MMM-dd-yy hh:mma').parse(dateprefix + " " + sp[0]);
        } else {
          date = DateFormat('MMM-dd-yy hh:mma').parse(sp[0] + " " + sp[1]);
          dateprefix = sp[0];
          if (earliest.isAfter(date)) {
            done = true;
            break;
          }
        }

        //headline += td.text + "--";
        break;
      }
      if (done) {
        break;
      }
      for (Element a in ele.getElementsByTagName("a")) {
        //print("a : " + a.text);
        link = a.attributes['href'] as String;
        print("actual headline: " + a.text);
        var content = await getArticleContent(link);
        if (content.isNotEmpty) {
          extract = content;
        } else {
          extract = a.text;
        }

        headline += a.text;
      }
      list.add(new Headline(date, headline, link, extract));
    }
    map[tick] = list;
  }

  return map;
}