颤动:检查列表中是否已存在对象

时间:2019-10-18 15:45:04

标签: flutter dart

我有以下代码

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();
  }
}

我已经开始了这样的课程

final FavoriteList favlist = new FavoriteList();,我使用来自json的以下代码填充favlist

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);
   });
 }

问题

我想做的是在添加之前检查favoriteitem中是否已经存在对象favlist

我尝试使用-

favlist.items.contains favlist.items.indexof但不起作用

我是扑扑/飞镖的新手,有人可以帮我吗

5 个答案:

答案 0 :(得分:4)

尝试一下,添加了两个方法,它的contains()应该可以正常工作

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);
}

答案 1 :(得分:2)

您可以执行以下操作:

constructor(
  @Inject(CACHE_MANAGER) private cacheManager
) {}

@Get()
async get(): Promise<any> {
  const customers = await this.cacheManager.wrap(
    '/customers',
    function() {
      return service.longRunningOperation()
    },
    { ttl: 600 }
  )
  return customers
}

答案 2 :(得分:0)

favlist.items.containsfavlist.items.indexof无法正常工作,因为我假设您正在检查favoriteitem是否存在(它永远不会消失,因为它是您刚刚创建的全新对象)。我建议通过一些唯一的标识符进行检查。在不了解您的项目太多的情况下,我会提出类似以下的建议:

假设每个收藏项的链接字段都是唯一的,则以下内容应会有所帮助:

//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);

如果existingItemnull,则列表中没有与该链接匹配的内容,否则将返回与该链接匹配的第一项。

答案 3 :(得分:0)

为避免两次添加同一项目,您可以尝试从列表(如果确实存在)中删除该项目,然后添加该项目,如下所示:

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

然后添加内容:

favlist.item.add(favouriteItem)

答案 4 :(得分:0)

我知道我参加晚会很晚,我认为API可能已更改。

现在,很容易在HiveObject上使用名为hiveObject.isInBox的布尔属性来检查它是否在盒子中。

这真的很好。