fromDatastoreKey method

  1. @override
Key fromDatastoreKey(
  1. Key datastoreKey
)
override

Converts a ds.Key to a Key. The key returned will have the correct id type which is either Key<String> or Key<int>.

Implementation

@override
Key fromDatastoreKey(ds.Key datastoreKey) {
  var namespace = Partition(datastoreKey.partition.namespace);
  var key = namespace.emptyKey;
  for (var element in datastoreKey.elements) {
    var type = _type2ModelDesc[_kind2ModelDesc[element.kind]!];
    if (type == null) {
      throw StateError(
          'Could not find a model associated with kind "${element.kind}". '
          'Please ensure a model class was annotated with '
          '`@Kind(name: "${element.kind}")`.');
    }
    final elementId = element.id;
    if (elementId is String) {
      key = key.append<String>(type, id: elementId);
    } else if (elementId is int) {
      key = key.append<int>(type, id: elementId);
    } else {
      throw StateError('Key must be either String or int, but '
          'was ${elementId.runtimeType} for key ${element.kind}');
    }
  }
  return key;
}