flutter

How to avoid duplicated headlines from news api?


I am using a news api where I am often getting many duplicate headlines. How can I adapt my code to avoid this?

final response = await http.get(url);

    final jsonData = jsonDecode(response.body);

    if (jsonData[Strings.status] == Strings.ok) {
      jsonData[Strings.articles].forEach((element) {
        if (element[Strings.urlToImg] != null &&
            element[SharedStrings.description] != null) {
          ArticleModel articleModel = ArticleModel(
            title: element[SharedStrings.title],
            author: element[Strings.author],
            description: element[SharedStrings.description],
            url: element[Strings.urlText],
            urlToImage: element[Strings.urlToImg],
            content: element[Strings.content], //context
          );
          news.add(articleModel);
getNews() async {
    News newsClass = News();
    await newsClass.getNews();
    articles = newsClass.news;
    setState(() => _loading = false);
  }

Solution

  • before the line

    news.add(articleModel);
    

    Add this:

    var isDuplicated = false;
    for (var new in news) {
       if (new.title == articleModel.title) {
          isDuplicated = true;
       }
    }
    
    if (!isDuplicated) {
       // Now you can add it
       news.add(articleModel);
    }