Allow building without ONNX Runtime

flutter_onnxruntime downloads ORT natively at build time, and the Linux and
Windows downloads from github.com/microsoft/onnxruntime are unavailable.
scripts/onnx.sh off swaps in a pure-Dart stub via pubspec_overrides.yaml, so no
platform pulls ORT; image messages then show as unavailable, as on web. Linux
CI builds use this mode for now.
This commit is contained in:
zjs81 committed 2026-11-20 16:38:09 -07:00
1 parent 34ea0cb7ca
commit d7fff0533e
12 files changed
+215 -13

No files matched your search

+24 -5
View File
@@ -871,14 +871,33 @@ class ImageCodecCancelled implements Exception {
String toString() => 'Image processing stopped.';
}
/// Whether this build can run the image codec at all.
///
/// False on web and in a build made with `scripts/onnx.sh off`, where
/// `flutter_onnxruntime` is the pure-Dart stub whose [OnnxRuntime] constructor
/// throws [UnsupportedError]. The real constructor has no side effects.
final bool imageCodecRuntimeAvailable = _probeOnnxRuntime();
bool _probeOnnxRuntime() {
if (kIsWeb) {
return false;
}
try {
OnnxRuntime();
return true;
} on UnsupportedError {
return false;
}
}
/// Factory used by the codec worker isolate.
///
/// Returns `null` on web, where there is no local model file and no isolate.
/// Everywhere else it returns the real [OnnxImageCodecBackend]; the session
/// turns a `null` here into an [ImageCodecUnimplemented], which the service
/// surfaces as `lastError` and maps to `ImageCodecAvailability.unavailable`.
/// Returns `null` when [imageCodecRuntimeAvailable] is false. Everywhere else
/// it returns the real [OnnxImageCodecBackend]; the session turns a `null`
/// here into an [ImageCodecUnimplemented], which the service surfaces as
/// `lastError` and maps to `ImageCodecAvailability.unavailable`.
ImageCodecBackend? createImageCodecBackend() {
if (kIsWeb) {
if (!imageCodecRuntimeAvailable) {
return null;
}
return OnnxImageCodecBackend();
+9 -3
View File
@@ -8,7 +8,8 @@ import '../models/image_codec_support.dart';
import '../utils/app_logger.dart';
import '../widgets/image_send_codec_binding.dart';
import 'app_settings_service.dart';
import 'image_codec_backend.dart' show kImageCodecBitstreamPathAvailable;
import 'image_codec_backend.dart'
show imageCodecRuntimeAvailable, kImageCodecBitstreamPathAvailable;
import 'image_codec_file_store.dart';
import 'image_codec_session.dart';
import 'image_codec_settings_store.dart';
@@ -177,9 +178,10 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
/// Why [availability] is `unavailable`, or null when it is not.
///
/// EXACTLY THREE CAUSES, all of them permanent properties of the build or the
/// EXACTLY FOUR CAUSES, all of them permanent properties of the build or the
/// platform, none of them fixable by the user:
/// * web, which has no native inference runtime;
/// * a build made without ONNX Runtime ([imageCodecRuntimeAvailable]);
/// * a build compiled without the entropy path
/// ([kImageCodecBitstreamPathAvailable]);
/// * a backend that failed to load.
@@ -194,6 +196,10 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
return 'The image codec needs a native inference runtime, which the web '
'build does not have.';
}
if (!imageCodecRuntimeAvailable) {
return 'This build was made without ONNX Runtime, so it cannot send or '
'show image messages.';
}
if (!kImageCodecBitstreamPathAvailable) {
return 'This build ships the image decoder only. Encoding and decoding a '
'bitstream also needs the entropy-side graph and the rANS coder, '
@@ -510,7 +516,7 @@ class ImageCodecService extends ChangeNotifier implements ImageSendCodec {
/// works. [canEncode]/[canDecode] additionally require the entropy path.
bool get canRunInference {
final model = selectedModel;
return !kIsWeb &&
return imageCodecRuntimeAvailable &&
!_backendMissing &&
model != null &&
model.localPath.isNotEmpty;