flutter: check if object already exist in the list

Viewed 17059

I have following code

class FavoriteItem {
  String headline;
  String content;
  String link;
  String publisheddate;

  FavoriteItem({this.headline, this.content, this.link, this.publisheddate});

  toJSONEncodable() {
    Map<String, dynamic> m = new Map();

    m['headline'] = headline;
    m['content'] = content;
    m['link'] = link;
    m['publisheddate'] = publisheddate;

    return m;
  }
}


class FavoriteList {
  List<FavoriteItem> items;

  FavoriteList() {
    items = new List();
  }

  toJSONEncodable() {
    return items.map((item) {
      return item.toJSONEncodable();
    }).toList();
  }
}

And i have initiated the class like this

final FavoriteList favlist = new FavoriteList(); and i populate favlist with following code from json

if (items != null) {
   (items as List).forEach((item) {
     final favoriteitem =  new FavoriteItem(headline: item['headline'], content: item['content'], link: item['link'], publisheddate: item['publisheddate']);
     favlist.items.add(favoriteitem);
   });
 }

Problem

What I want to do is to check if object favoriteitem already exist in favlist before adding.

I tried using -

favlist.items.contains favlist.items.indexof but didn't work

I am new to flutter/dart, can anybody please help me on this

5 Answers

favlist.items.contains and favlist.items.indexof are not working because I assume you are checking if favoriteitem exists (which it never will because it is a brand new object you just created). I would suggest checking by some unique identifier. Without knowing too much about your project, I would suggest something like the following:

Assuming your link field is unique per favorite item, the following should help:

//this is your new created favoriteitem to check against
final favoriteitem =  new FavoriteItem(headline: item['headline'], content: item['content'], link: item['link'], publisheddate: item['publisheddate']);

//find existing item per link criteria
var existingItem = items.firstWhere((itemToCheck) => itemToCheck.link == favoriteitem.link, orElse: () => null);

If existingItem is null, then nothing exists in your list that matches that link, otherwise it will return the first item matching that link.

You could do something like this:

listB.addAll(listA.where((a) => listB.every((b) => a.id != b.id)));

try this, two method were added, with it contains() should work properly

class FavoriteItem {
  String headline;
  String content;
  String link;
  String publisheddate;

  FavoriteItem({this.headline, this.content, this.link, this.publisheddate});

  toJSONEncodable() {
    Map<String, dynamic> m = new Map();

    m['headline'] = headline;
    m['content'] = content;
    m['link'] = link;
    m['publisheddate'] = publisheddate;

    return m;
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is FavoriteItem &&
          runtimeType == other.runtimeType &&
          headline == other.headline &&
          content == other.content &&
          link == other.link &&
          publisheddate == other.publisheddate;

  @override
  int get hashCode => hashValues(headline, content, link, publisheddate);
}

I know I am late to the party and I think the API might have changed.

Now it is easy as using a boolean property called hiveObject.isInBox on a HiveObject to check if it is inside a box or not.

This works really well.

In order not to add the same item twice, you can just try deleting the item from the list (if it exist at all) before adding the item as seen below:

favlist.item.removeWhere((it) => it.header == favouriteItem.header && it.content ==favouriteItem.content && it.link == favouriteItem.link ...)

then add the content afterwards:

favlist.item.add(favouriteItem)
Related