ensureUniqueKeysForList method Null safety

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 == null || items.isEmpty) {
    return items;
  }

  final List<Widget> itemsWithUniqueKeys = <Widget>[];
  int itemIndex = baseIndex;
  for (final Widget item in items) {
    itemsWithUniqueKeys.add(KeyedSubtree.wrap(item, itemIndex));
    itemIndex += 1;
  }

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