debugItemsHaveDuplicateKeys function

bool debugItemsHaveDuplicateKeys (Iterable<Widget> items)

Asserts if the given list of items contains any duplicate non-null keys.

To invoke this function, use the following pattern:

assert(!debugItemsHaveDuplicateKeys(items));

For a version of this function specifically intended for parents checking their children lists, see debugChildrenHaveDuplicateKeys.

Does nothing if asserts are disabled. Always returns true.

Implementation

bool debugItemsHaveDuplicateKeys(Iterable<Widget> items) {
  assert(() {
    final Key nonUniqueKey = _firstNonUniqueKey(items);
    if (nonUniqueKey != null)
      throw FlutterError('Duplicate key found: $nonUniqueKey.');
    return true;
  }());
  return false;
}