getOrtValueData method

  1. @override
Future<Map<String, dynamic>> getOrtValueData(
  1. String valueId
)

Gets the data from an OrtValue

valueId is the ID of the OrtValue to get data from

Implementation

@override
Future<Map<String, dynamic>> getOrtValueData(String valueId) async {
  try {
    // Check if the tensor exists
    if (!_ortValues.containsKey(valueId)) {
      throw PlatformException(code: "INVALID_VALUE", message: "OrtValue not found with ID: $valueId", details: null);
    }

    final tensor = _ortValues[valueId]!;

    // Get tensor type
    final type = tensor.getProperty('type'.toJS);

    // Get tensor shape
    final jsShape = tensor.getProperty('dims'.toJS) as JSObject;
    final shapeLength = (jsShape.getProperty('length'.toJS) as JSNumber).toDartInt;
    final shape = <int>[];

    // Convert shape to Dart list
    for (var i = 0; i < shapeLength; i++) {
      final dim = jsShape.getProperty(i.toString().toJS) as JSNumber;
      shape.add(dim.toDartInt);
    }

    // Get tensor data
    final jsData = tensor.getProperty('data'.toJS) as JSObject;
    final dataLength = (jsData.getProperty('length'.toJS) as JSNumber).toDartInt;

    // Convert data to Dart TypedData based on type
    final dynamic data;
    switch (type.toString()) {
      case 'float32':
        // Return Float32List to preserve float32 precision
        final tempList = <double>[];
        for (var i = 0; i < dataLength; i++) {
          final val = jsData.getProperty(i.toString().toJS) as JSNumber;
          tempList.add(val.toDartDouble);
        }
        data = typed_data.Float32List.fromList(tempList);
        break;

      case 'int32':
        // Return Int32List to preserve int32 type
        final tempList = <int>[];
        for (var i = 0; i < dataLength; i++) {
          final val = jsData.getProperty(i.toString().toJS) as JSNumber;
          tempList.add(val.toDartInt);
        }
        data = typed_data.Int32List.fromList(tempList);
        break;

      case 'int64':
        // Return Int64List to preserve int64 type
        // BigInt handling
        final tempList = <int>[];
        for (var i = 0; i < dataLength; i++) {
          final val = jsData.getProperty(i.toString().toJS) as JSNumber;
          // Convert BigInt or similar to standard number if possible
          // For dart:js_interop, try direct conversion to num or int
          tempList.add(val.toDartInt);
        }
        data = typed_data.Int64List.fromList(tempList);
        break;

      case 'uint8':
        // Return Uint8List to preserve uint8 type
        final tempList = <int>[];
        for (var i = 0; i < dataLength; i++) {
          final val = jsData.getProperty(i.toString().toJS) as JSNumber;
          tempList.add(val.toDartInt);
        }
        data = typed_data.Uint8List.fromList(tempList);
        break;

      case 'bool':
        // Return List<dynamic> for bool (no TypedData equivalent)
        final boolList = <dynamic>[];
        for (var i = 0; i < dataLength; i++) {
          // For boolean tensors, convert the numeric values back to proper Dart booleans
          final val = jsData.getProperty(i.toString().toJS) as JSNumber;
          // Return the actual numeric value (1 or 0), not a boolean,
          // to match native implementations which return 1/0
          boolList.add(val.toDartInt != 0 ? 1 : 0);
        }
        data = boolList;
        break;

      case 'string':
        // Return List<String> for string (no TypedData equivalent)
        final stringList = <dynamic>[];
        for (var i = 0; i < dataLength; i++) {
          final val = jsData.getProperty(i.toString().toJS) as JSString;
          // For string tensors, extract string values
          stringList.add(val.toString());
        }
        data = stringList;
        break;

      default:
        throw PlatformException(
          code: "UNSUPPORTED_TYPE",
          message: "Unsupported data type for extraction: $type",
          details: null,
        );
    }

    return {'data': data, 'shape': shape};
  } catch (e) {
    if (e is PlatformException) {
      rethrow;
    }
    throw PlatformException(code: "DATA_EXTRACTION_ERROR", message: "Failed to get OrtValue data: $e", details: null);
  }
}