flutter-examples/README.md
This directory contains Flutter examples for sherpa-onnx.
Start here: Read
hello_worldfirst to learn how to initialize sherpa-onnx in a Flutter app. Then readvad-from-microphoneorvad-from-fileto learn how to copy model files from assets to a writable directory — this is required for all examples that use models.
| Directory | Description |
|---|---|
| hello_world | Minimal example — prints sherpa-onnx version info |
| streaming_asr | Streaming (online) speech recognition |
| non_streaming_vad_asr | VAD + non-streaming speech recognition |
| tts | Text to speech |
| vad-from-file | Voice activity detection from an audio file |
| vad-from-microphone | Voice activity detection from microphone |
| vad-non-streaming-asr-from-file | VAD + non-streaming ASR from an audio file |
| vad-non-streaming-asr-from-microphone | VAD + non-streaming ASR from microphone |
| offline-punctuation | Offline punctuation restoration |
| online-punctuation | Online punctuation restoration |
All Flutter examples initialize sherpa-onnx the same way:
import 'package:sherpa_onnx/sherpa_onnx.dart' as sherpa_onnx;
await sherpa_onnx.initBindingsAsync();
No path argument is needed. The native library is linked into the app bundle by the Flutter build system.
Isolates: Each isolate has its own FFI binding state. You must call
initBindings() or initBindingsAsync() in every isolate that uses
sherpa-onnx APIs. Calling it in one isolate does NOT make sherpa-onnx
available in other isolates. See the
vad-from-microphone example for a working
isolate pattern.
Flutter apps run in a sandbox and cannot access arbitrary file paths. Model files must be bundled as Flutter assets and copied to a writable directory at runtime.
Step 1: Add the model to pubspec.yaml:
flutter:
assets:
- assets/model.onnx
Step 2: Copy the model to a writable directory:
import 'dart:io';
import 'package:flutter/services.dart' show rootBundle;
import 'package:path_provider/path_provider.dart';
Future<String> copyAsset(String assetPath, String fileName) async {
final dir = await getApplicationDocumentsDirectory();
final file = File('${dir.path}/$fileName');
if (!await file.exists()) {
final data = await rootBundle.load(assetPath);
await file.writeAsBytes(data.buffer.asUint8List());
}
return file.path;
}
Step 3: Use the copied path in your config:
final modelPath = await copyAsset('assets/model.onnx', 'model.onnx');
See vad-from-microphone or
vad-from-file for complete working examples.
cd hello_world
flutter pub get
flutter run -d macos
Connect your iPhone, then:
flutter run -d <device-id>
You need a valid Apple Developer certificate for device deployment.
For iOS apps that use the microphone, add NSMicrophoneUsageDescription
to ios/Runner/Info.plist.
flutter run -d <device-id>
If you get a minSdk error, update android/app/build.gradle:
android {
defaultConfig {
minSdk = 23
}
}
flutter config --enable-linux-desktop
flutter run -d linux
flutter run -d windows
flutter run -d chrome
Download pre-built Flutter apps for different platforms at https://github.com/k2-fsa/sherpa-onnx/releases/tag/flutter
Try the following demos directly in your browser:
| Demo | URL |
|---|---|
| VAD from file | https://modelscope.cn/studios/csukuangfj/wasm-vad-from-file |
| VAD from microphone | https://modelscope.cn/studios/csukuangfj/wasm-vad-from-microphone |
| Offline punctuation | https://modelscope.cn/studios/csukuangfj/wasm-offline-punctuation |
| Online punctuation | https://modelscope.cn/studios/csukuangfj/wasm-online-punctuation |