findOrFail<T extends V> method

T findOrFail<T extends V>(
  1. bool callback(
    1. V element
    ), {
  2. String? message,
})

Returns the first element satisfying test, or throw if there are none.

Example :

Channel? channel = guild.channels.cache.find((channel) => channel.id == '991686152585232404');
print(channel);

You can define an error customized message

Example :

Channel channel = guild.channels.cache.find((channel) => channel.id == '991686152585232404', message: 'Channel is undefined');
print(channel);

Implementation

T findOrFail<T extends V> (bool Function(V element) callback, { String? message }) {
  final V? result = find(callback);
  if (result == null) {
    throw NotExistException(message ?? 'No values were found.');
  }

  return result as T;
}