toScalar method Null safety

T toScalar()

Returns a scalar when the tensor has only a single element;

Throws StateError the shape is not a scalar.

Implementation

T toScalar() {
  final tensorShape = this.tensorShape;
  if (tensorShape == TensorShape.scalar) {
    return elements.single;
  }
  throw UnsupportedError('Shape of the tensor is not a scalar: $tensorShape');
}