optionalAttribute<T> method

Future<T?> optionalAttribute<T>(
  1. String attributeName, {
  2. Converter<T>? convert,
})

Extracts an attribute by name. Returns null if not found.

T The type to return. Specify as e.g. attribute<double>(...)) Defaults to String. attributeName the name of the attribute to find. Accepts fully qualified or unqualified names. converter A function of the form T Function(String) that parses the string to an object. Not needed for any of the primitive types or Uri. (See autoConverter)

Implementation

Future<T?> optionalAttribute<T>(String attributeName,
    {Converter<T>? convert}) async {
  convert = convert ?? autoConverter(T);
  try {
    return convert(attributes
        .firstWhere((a) =>
            (a.name == attributeName) ||
            (_stripNamespace(a.name) == attributeName))
        .value);
  } catch (_) {
    return null;
  }
}