getOrtValueData method
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 = getProperty(tensor, 'type');
// Get tensor shape
final jsShape = getProperty(tensor, 'dims');
final shapeLength = getProperty(jsShape, 'length') as int;
final shape = <int>[];
// Convert shape to Dart list
for (var i = 0; i < shapeLength; i++) {
shape.add(callMethod(jsShape, 'at', [i]) as int);
}
// Get tensor data
final jsData = getProperty(tensor, 'data');
final dataLength = getProperty(jsData, 'length') as int;
final data = <dynamic>[];
// Convert data to Dart list based on type
switch (type.toString()) {
case 'float32':
for (var i = 0; i < dataLength; i++) {
data.add((callMethod(jsData, 'at', [i]) as num).toDouble());
}
break;
case 'int32':
for (var i = 0; i < dataLength; i++) {
data.add((callMethod(jsData, 'at', [i]) as num).toInt());
}
break;
case 'int64':
// BigInt handling
for (var i = 0; i < dataLength; i++) {
final value = callMethod(jsData, 'at', [i]);
// Convert BigInt or similar to standard number if possible
data.add(js_util.dartify(value));
}
break;
case 'uint8':
for (var i = 0; i < dataLength; i++) {
data.add((callMethod(jsData, 'at', [i]) as num).toInt());
}
break;
case 'bool':
for (var i = 0; i < dataLength; i++) {
// For boolean tensors, convert the numeric values back to proper Dart booleans
final value = callMethod(jsData, 'at', [i]);
// Convert JS value to Dart numeric value before comparison
final numValue = js_util.dartify(value);
// Return the actual numeric value (1 or 0), not a boolean,
// to match native implementations which return 1/0
data.add(numValue != 0 ? 1 : 0);
}
break;
case 'string':
for (var i = 0; i < dataLength; i++) {
// For string tensors, extract string values
final value = callMethod(jsData, 'at', [i]);
data.add(value.toString());
}
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);
}
}