ensureUniqueKeysForList static method

List<Widget> ensureUniqueKeysForList(
  1. List<Widget> items, {
  2. int baseIndex = 0,
})

Wrap each item in a KeyedSubtree whose key is based on the item's existing key or the sum of its list index and baseIndex.

Implementation

static List<Widget> ensureUniqueKeysForList(List<Widget> items, { int baseIndex = 0 }) {
  if (items.isEmpty) {
    return items;
  }

  final List<Widget> itemsWithUniqueKeys = <Widget>[
    for (final (int i, Widget item) in items.indexed)
      KeyedSubtree.wrap(item, baseIndex + i),
  ];

  assert(!debugItemsHaveDuplicateKeys(itemsWithUniqueKeys));
  return itemsWithUniqueKeys;
}