Метод putIfAbsent
- String key,
- dynamic ifAbsent( )
override
Ищет значение key, или добавляет новую запись, если ее нет.
Возвращает значение, связанное с key, если оно существует. В противном случае вызывает ifAbsent, чтобы получить новое значение, связывает key с этим значением и возвращает новое значение.
final diameters = <num, String>{1.0: 'Earth'};
final otherDiameters = <double, String>{0.383: 'Mercury', 0.949: 'Venus'};
for (final item in otherDiameters.entries) {
diameters.putIfAbsent(item.key, () => item.value);
}
print(diameters); // {1.0: Earth, 0.383: Mercury, 0.949: Venus}
// If the key already exists, the current value is returned.
final result = diameters.putIfAbsent(0.383, () => 'Random');
print(result); // Mercury
print(diameters); // {1.0: Earth, 0.383: Mercury, 0.949: Venus} Вызов ifAbsent не должен добавлять или удалять ключи из карты.
Реализация
dynamic putIfAbsent(String key, dynamic ifAbsent()) {
throw new UnsupportedError("Not supported");
}
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.18.5/dart-web_audio/AudioParamMap/putIfAbsent.html