← Back to posts Cover image for Mastering On-Device AI in Flutter: Integrating Local LLMs and Machine Learning

Mastering On-Device AI in Flutter: Integrating Local LLMs and Machine Learning

· 5 min read
Weekly Digest

The Flutter news you actually need

No spam, ever. Unsubscribe in one click.

Chris
By Chris

Bringing AI to Your Flutter App: A Practical Guide to On-Device Processing

You’ve got a great app idea. Maybe it needs to classify images, summarize text, or answer simple questions. The obvious path is to build a backend, host a large model, and handle API calls. But what if you could skip all that? What if the AI could run directly on the user’s phone—working offline, costing you nothing per query, and respecting user privacy? This is the promise of on-device AI, and with Flutter, it’s more accessible than you think.

The core challenge is balancing capability with practicality. You can’t just slap a 10-billion-parameter model into your app. Instead, we focus on “small AI features”: specialized, efficient models that do one thing well. Let’s explore the tools and patterns that make this possible.

The Toolkit: Frameworks for Flutter

Several excellent packages bridge the gap between machine learning frameworks and Flutter. Your choice depends on your task.

1. For Vision & Audio: tflite_flutter For tasks like object detection, face landmarking, or audio classification, TensorFlow Lite is a go-to. The tflite_flutter package provides a robust wrapper.

import 'package:tflite_flutter/tflite_flutter.dart';

class ImageClassifier {
  late Interpreter _interpreter;

  Future<void> loadModel() async {
    try {
      final delegate = Platform.isAndroid
          ? GpuDelegateV2()
          : Platform.isIOS
              ? GpuDelegate()
              : null;

      final options = InterpreterOptions();
      if (delegate != null) {
        options.addDelegate(delegate);
      }

      // Load your .tflite model from assets
      _interpreter = await Interpreter.fromAsset('mobilenet_v2.tflite',
          options: options);
    } catch (e) {
      print('Failed to load model: $e');
    }
  }

  List<double> classifyImage(List<List<List<double>>> imageData) {
    // Define input and output shapes based on your model
    final input = [imageData];
    final output = [List<double>.filled(1000, 0)]; // Example: 1000 classes

    _interpreter.run(input, output);
    return output[0];
  }
}

2. For Generative Text: Lightweight LLMs Running a local Large Language Model is more demanding but feasible for smaller models (like Phi-2, TinyLlama, or Gemma 2B). The flutter_llm package (or similar bindings to llama.cpp) can be a starting point. Be warned: this significantly increases your app size (100MB+) and requires careful performance testing.

3. The Hybrid Approach: Firebase ML If you want some of the benefits of on-device processing without managing model files in your bundle, Firebase Machine Learning is a fantastic middle ground. You can host your TensorFlow Lite models dynamically in Firebase, and the SDK will download and cache them on the device.

import 'package:firebase_ml_model_downloader/firebase_ml_model_downloader.dart';

Future<File> getFirebaseModel(String modelName) async {
  final conditions = FirebaseModelDownloadConditions(
    iosAllowsCellularAccess: true,
    iosAllowsBackgroundDownloading: true,
  );

  // Firebase handles downloading, updates, and caching
  final customModel =
      await FirebaseModelDownloader.getInstance().getModel(
    modelName,
    FirebaseModelDownloadType.localModelUpdateInBackground,
    conditions,
  );
  return File(customModel.file.path);
}

Making the Right Choice: Trade-offs and Guidance

Your decision tree should look something like this:

  • Task: Image Labeling, Object Detection, Pose Estimation

    • Best Tool: tflite_flutter with a custom .tflite model.
    • Why: Highly optimized, small model size (often <10MB), excellent performance.
  • Task: Text Summarization, Simple Q&A, Creative Writing

    • Best Tool: A lightweight LLM runtime (e.g., via dart:ffi bindings).
    • Why: Needs the generative capability of an LLM. Be prepared for slower inference (seconds) and a large app bundle.
  • Task: Any of the above, but you need to update models frequently without app releases.

    • Best Tool: Firebase ML.
    • Why: It decouples model deployment from app deployment. You can push a new model version via the Firebase console, and it rolls out to users seamlessly.
  • Task: Real-time video processing (e.g., filters, segmentation).

    • Best Tool: MediaPipe for Flutter.
    • Why: MediaPipe is built for efficient, real-time media pipelines with pre-built, high-quality solutions.

Common Pitfalls and How to Avoid Them

  1. Ignoring App Size: Always check your final APK/IPA size. A 150MB model might be a non-starter for mobile data downloads. Use Firebase ML for dynamic delivery or choose the smallest model that meets your accuracy needs.
  2. Blocking the UI: Model inference is computationally heavy. Never run it on the main thread. Always use isolates or the built-in async methods provided by your ML package.
    // DO THIS: Run inference in an isolate or background thread
    Future<List<double>> classifyImageIsolate(List imageData) async {
      return await compute(_runInference, imageData);
    }
    
    static List<double> _runInference(List imageData) {
      // Your interpreter.run() logic here
    }
  3. Forgetting Platform Variance: Performance varies wildly between devices. An iPhone 15 will run circles around a mid-range Android from 2020. Implement a performance benchmark on app start if necessary, and consider offering a “low-precision” mode for older devices.

Getting Started: Your First On-Device Feature

A solid first project is an image classifier.

  1. Find a pre-trained model (TensorFlow Hub is a great source) and convert it to .tflite.
  2. Add the tflite_flutter package and your model file to assets/.
  3. Write a simple service class (like the ImageClassifier above) to load and run the model.
  4. Feed it images from image_picker, pre-process them (resize, normalize), and display the top result.

By starting small, you learn the entire pipeline: model acquisition, integration, and performance profiling. This foundation lets you confidently add smarter, more responsive features that make your app stand out—all without a single server bill.

This blog is produced with the assistance of AI by a human editor. Learn more

Related Posts

Cover image for Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Choosing the Right Storage for Flutter: Beyond SharedPreferences for Persistent Data

Developers frequently struggle with persistent storage in Flutter, especially when dealing with OS-level backups and app reinstalls. This post will clarify the nuances of different storage solutions like `SharedPreferences`, `path_provider`, and `flutter_secure_storage`, focusing on how to correctly manage data lifetimes (per-install vs. per-user) and avoid issues like device IDs migrating to new phones or data being unexpectedly backed up.

Cover image for Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Optimizing Flutter UI Performance: Solving `ImageFiltered` Lag and Smooth Blurring

Flutter's `ImageFiltered` can cause significant lag when blurring large or frequently changing content like text. This post will explain the performance bottlenecks, provide solutions like caching blurred outputs with `RepaintBoundary` or `SnapshotWidget`, and offer practical tips to ensure smooth UI effects without sacrificing frame rates.