flutter_onnxruntime

flutter_onnxruntime

Native Wrapper Flutter Plugin for ONNX Runtime

Current supported ONNX Runtime version: 1.28.0

Note: For Android build, you need to upgrade your flutter_onnxruntime to version >=1.5.1 to satisfy the 16 KB Google Play compatibility requirement.

๐ŸŒŸ Why This Project?

flutter_onnxruntime is a lightweight plugin that provides native wrappers for running ONNX Runtime on multiple platforms.

  ๐Ÿ“ฆ No Pre-built Libraries
  Libraries are fetched directly from official repositories during installation, ensuring they are always up-to-date!

  ๐Ÿ›ก๏ธ Memory Safety
  All memory management is handled in native code, reducing the risk of memory leaks.

  ๐Ÿ”„ Easy Upgrades
  Stay current with the latest ONNX Runtime releases without the hassle of maintaining complex generated FFI wrappers.

๐Ÿš€ Getting Started

Installation

Add the following dependency to your pubspec.yaml:

dependencies:
  flutter_onnxruntime: ^1.8.1

Quick Start

Example of running an addition model:

import 'package:flutter_onnxruntime/flutter_onnxruntime.dart';

// create inference session
final ort = OnnxRuntime();
final session = await ort.createSessionFromAsset('assets/models/addition_model.onnx');

// specify input with data and shape
final inputs = {
   'A': await OrtValue.fromList([1, 1, 1], [3]),
   'B': await OrtValue.fromList([2, 2, 2], [3])
}

// start the inference
final outputs = await session.run(inputs);

// print output data
print(await outputs['C']!.asList());

To get started with the Flutter ONNX Runtime plugin, see the API Usage Guide.

๐Ÿงช Examples

Simple Addition Model

A simple model with only one operator (Add) that takes two inputs and produces one output.

Run this example with:

cd example
flutter pub get
flutter run

Image Classification Model

A more complex model that takes an image as input and classifies it into one of the predefined categories.

Clone this repository and run the example following the repo's guidelines.

๐Ÿ“Š Component Overview

Component Description
OnnxRuntime Main entry point for creating sessions and configuring global options
OrtSession Represents a loaded ML model for running inference
OrtValue Represents tensor data for inputs and outputs
OrtSessionOptions Configuration options for session creation
OrtRunOptions Configuration options for inference execution

๐Ÿšง Implementation Status

Feature Android iOS Linux macOS Windows Web
CPU Inference โœ… โœ… โœ… โœ… โœ… โœ…
EP1 Configuration โœ… โœ… โœ… โœ… โœ… โœ…
Input/Output names โœ… โœ… โœ… โœ… โœ… โœ…
Data Type Conversion โœ… โœ… โœ… โœ… โœ… โœ…
Inference on Emulator โœ… โœ… โœ… โœ… โœ… โœ…
Input/Output Info โœ… โŒ* โœ… โŒ* โœ… โœ…
Model Metadata โœ… โŒ* โœ… โŒ* โœ… โŒ*
FP16 Support โœ… โœ… โœ๏ธ โœ… โœ๏ธ โœ๏ธ

โœ…: Completed

โŒ: Not supported

๐Ÿšง: Ongoing

โœ๏ธ: Planned

*: Retrieving model metadata and input/output info is not available for Swift and Javascript API.

1: Execution Providers (EP) are hardware accelerated inference interface for AI inference (e.g., CPU, GPU, NPU, TPU, etc.)

๐Ÿ“‹ Required development setup

Android

Android build requires proguard-rules.pro inside your Android project at android/app/ with the following content:

-keep class ai.onnxruntime.** { *; }

or running the below command from your terminal:

echo "-keep class ai.onnxruntime.** { *; }" > android/app/proguard-rules.pro

Refer to troubleshooting.md for more information.

iOS and macOS

ONNX Runtime requires at least iOS 16 and macOS 14. Open ios/Runner.xcworkspace or macos/Runner.xcworkspace in Xcode, then in Runner โ†’ General set Minimum Deployments to 16.0 (iOS) or 14.0 (macOS).

Use Swift Package Manager (SPM). It needs no Podfile changes. Flutter 3.44 and later turn it on by default. On Flutter 3.24 to 3.41, turn it on with:

flutter config --enable-swift-package-manager

CocoaPods still works, but we no longer recommend it:

  • Flutter has made SPM the default.
  • CocoaPods trunk becomes read-only on 2 December 2026.
  • A clean pod install currently fails because Microsoft's download host is down (see the Microsoft download outage).

If you stay on CocoaPods, see CocoaPods setup for the Podfile changes.

Linux and Windows

The build downloads ONNX Runtime 1.28.0 from masicai/onnxruntime-binaries and checks its SHA-256. Microsoft's GitHub release downloads have been unavailable since November 2026 (see the Microsoft download outage). That repository repackages the same binaries from Microsoft's signed NuGet package, unmodified.

To use another version or download location, set these as plain variables in your app's linux/CMakeLists.txt or windows/CMakeLists.txt, anywhere before the include(flutter/generated_plugins.cmake) line:

set(ONNXRUNTIME_VERSION "1.22.0")
set(ONNXRUNTIME_URL "https://example.com/onnxruntime-linux-x64-1.22.0.tgz")
set(ONNXRUNTIME_SHA256 "<SHA-256 of that archive>")
  • ONNXRUNTIME_VERSION alone only works for versions published in masicai/onnxruntime-binaries, which is currently 1.28.0.
  • ONNXRUNTIME_URL must point to an archive laid out like Microsoft's release archives: one top-level directory named after the archive, such as onnxruntime-linux-x64-1.22.0/, containing include/ and lib/. Microsoft's release archives (https://github.com/microsoft/onnxruntime/releases/download/v<version>/onnxruntime-<linux|win>-<arch>-<version>.<tgz|zip>) have this layout.
  • Without ONNXRUNTIME_SHA256, the build prints a warning with the archive's SHA-256 and doesn't verify the download.

Passing the variables with -D to CMake works too. With set(... CACHE ...) instead, changing them later needs flutter clean, because CMake never overwrites an existing cache entry.

๐Ÿ› ๏ธ Troubleshooting

For troubleshooting, see the troubleshooting.md file.

๐Ÿค Contributing

Contributions to the Flutter ONNX Runtime plugin are welcome. Please see the CONTRIBUTING.md file for more information.

๐Ÿ“š Documentation

Find more information in the documentation.