tryFind<T extends Card> method

T? tryFind<T extends Card>(
  1. int orderInSet, [
  2. String? debugAssertName
])

Looks up a card in this collection by its orderInSet.

If the card is not found, returns null.

If the card is found, but is not of type T, throws a StateError. For example, if you call tryFind<UnitCard>(1) on a collection that contains a BaseCard at order 1, an error will be thrown.

Implementation

T? tryFind<T extends Card>(int orderInSet, [String? debugAssertName]) {
  final card = _lookupCards[orderInSet - 1];
  if (card == null) {
    return null;
  }
  assert(
    debugAssertName == null || card.name == debugAssertName,
    'Expected $debugAssertName, got ${card.name}',
  );
  if (card is T) {
    return card;
  }
  throw StateError('Expected $T, got ${card.runtimeType}');
}