dart:async
庫中Stream.singleWhere
方法的用法介紹如下。
用法:
Future<T> singleWhere(
bool test(
T element
),
{T orElse(
)?}
)
在此流中查找與 test
匹配的單個元素。
與 lastWhere 類似,但如果此流中出現多個匹配元素則為錯誤。
例子:
var result = await Stream.fromIterable([1, 2, 3, 6, 9, 12])
.singleWhere((element) => element % 4 == 0, orElse: () => -1);
print(result); // 12
result = await Stream.fromIterable([2, 6, 8, 12, 24, 32])
.singleWhere((element) => element % 9 == 0, orElse: () => -1);
print(result); // -1
result = await Stream.fromIterable([2, 6, 8, 12, 24, 32])
.singleWhere((element) => element % 6 == 0, orElse: () => -1);
// Throws.
相關用法
- Dart Stream.skipWhile用法及代碼示例
- Dart Stream.skip用法及代碼示例
- Dart Stream.fromFutures用法及代碼示例
- Dart Stream.fold用法及代碼示例
- Dart Stream.map用法及代碼示例
- Dart Stream.asBroadcastStream用法及代碼示例
- Dart Stream.handleError用法及代碼示例
- Dart Stream.where用法及代碼示例
- Dart Stream.reduce用法及代碼示例
- Dart Stream.join用法及代碼示例
- Dart Stream.error用法及代碼示例
- Dart Stream.periodic用法及代碼示例
- Dart Stream.take用法及代碼示例
- Dart Stream.every用法及代碼示例
- Dart Stream.lastWhere用法及代碼示例
- Dart Stream.contains用法及代碼示例
- Dart Stream.eventTransformed用法及代碼示例
- Dart Stream.firstWhere用法及代碼示例
- Dart Stream.drain用法及代碼示例
- Dart Stream.empty用法及代碼示例
- Dart Stream.multi用法及代碼示例
- Dart Stream.distinct用法及代碼示例
- Dart Stream.timeout用法及代碼示例
- Dart Stream.transform用法及代碼示例
- Dart Stream.fromFuture用法及代碼示例
注:本文由純淨天空篩選整理自dart.dev大神的英文原創作品 singleWhere method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。