openCursor 方法

Stream<CursorWithValue> openCursor(
  1. {dynamic key,
  2. KeyRange? range,
  3. String? direction,
  4. bool? autoAdvance}
)

创建一个流,该流遍历该对象存储中的记录。

流项之后必须手动通过调用 Cursor.next 进行推进,或者通过将 autoAdvance 设置为 true 来指定自动推进。

var cursors = objectStore.openCursor().listen(
  (cursor) {
    // ...some processing with the cursor
    cursor.next(); // advance onto the next cursor.
  },
  onDone: () {
    // called when there are no more cursors.
    print('all done!');
  });

与当前事务无关的异步操作将导致事务自动提交--除非它们是对当前事务的附加异步请求,否则所有处理都必须同步进行。

实现

Stream<CursorWithValue> openCursor(
    {key, KeyRange? range, String? direction, bool? autoAdvance}) {
  var key_OR_range = null;
  if (key != null) {
    if (range != null) {
      throw new ArgumentError('Cannot specify both key and range.');
    }
    key_OR_range = key;
  } else {
    key_OR_range = range;
  }

  // TODO: try/catch this and return a stream with an immediate error.
  var request;
  if (direction == null) {
    request = _openCursor(key_OR_range);
  } else {
    request = _openCursor(key_OR_range, direction);
  }
  return _cursorStreamFromResult(request, autoAdvance);
}