getDailySentiment method Null safety

Map<String, Map<DateTime, SentimentResult>>? getDailySentiment(
  1. Map<String, List<Headline>> map
)

getDailySentiment method returns a sentiment result for each day worth of headlines

Implementation

Map<String, Map<DateTime, SentimentResult>>? getDailySentiment(
    Map<String, List<Headline>> map) {
  final sentiment = Sentiment();
  var mapResult = new Map<String, Map<DateTime, SentimentResult>>();
  map.forEach((key, value) {
    var singleMap = new Map<DateTime, SentimentResult>();
    int result = 0;
    int average = 0;
    int positive = 0;
    int negative = 0;
    int neutral = 0;
    DateTime currentDate = value[0].date;
    for (Headline headline in value) {
      DateTime now = headline.date;
      if (now.day != currentDate.day) {
        var avgscore = result / average;
        singleMap[currentDate] =
            new SentimentResult(avgscore, positive, negative, neutral);
        neutral = 0;
        positive = 0;
        negative = 0;
        result = 0;
        average = 0;
        currentDate = now;
      }
      var sent = sentiment.analysis(headline.extract);
      var score = sent["score"] as int;
      if (score == 0) {
        neutral++;
      } else if (score > 0) {
        positive++;
      } else {
        negative++;
      }
      //print(sent);
      result += score;
      average++;
    }
    var avgscore = result / average;
    singleMap[currentDate] =
        new SentimentResult(avgscore, positive, negative, neutral);
    mapResult[key] = singleMap;
  });
  return mapResult;
}