November 7, 2025
Flutter

How to use AI in Flutter Application

Using AI in a Flutter application involves integrating machine learning (ML) models or AI services to perform tasks like image recognition, text generation, speech-to-text, recommendation systems, or chatbots.

Here’s a step-by-step guide on how to use AI in a Flutter app:


1. Decide the Use Case

Some common AI features in Flutter apps:

  • Chatbots (e.g., OpenAI ChatGPT, Dialogflow)
  • Image recognition (e.g., TensorFlow Lite)
  • Text classification / sentiment analysis
  • Speech recognition / text-to-speech
  • Recommendation systems

🔹 2. Choose the AI Approach

You have two main options:

  1. Cloud-based AI services (e.g., OpenAI, Google ML Kit, Firebase ML, Dialogflow)
  2. On-device ML models (e.g., TensorFlow Lite, tflite_flutter)

3. Add Dependencies

A. Cloud-based AI Example

Using OpenAI GPT model:

dependencies:
  http: ^0.13.6
  flutter_dotenv: ^5.0.2

Store your API key securely using .env file.

Sample API Call to OpenAI:

import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:flutter_dotenv/flutter_dotenv.dart';

Future<String> getAIResponse(String prompt) async {
  final response = await http.post(
    Uri.parse("https://api.openai.com/v1/chat/completions"),
    headers: {
      "Content-Type": "application/json",
      "Authorization": "Bearer ${dotenv.env['OPENAI_API_KEY']}",
    },
    body: jsonEncode({
      "model": "gpt-3.5-turbo",
      "messages": [{"role": "user", "content": prompt}],
    }),
  );

  final decoded = jsonDecode(response.body);
  return decoded['choices'][0]['message']['content'];
}

B. On-device ML Example

Using TensorFlow Lite:

dependencies:
  tflite_flutter: ^0.10.2
  tflite_flutter_helper: ^0.4.0

Steps:

  1. Convert your ML model to .tflite format.
  2. Place it in assets/.
  3. Load and use it in your Dart code.
import 'package:tflite_flutter/tflite_flutter.dart';

Future<void> runModel() async {
  final interpreter = await Interpreter.fromAsset('model.tflite');
  var input = [1.0, 2.0]; // Example input
  var output = List.filled(1, 0).reshape([1]);

  interpreter.run(input, output);
  print('Output: $output');
}

4. Popular AI Libraries / APIs for Flutter

Use CaseTool / Package
ChatbotsOpenAI, Dialogflow, Rasa
Text/Speechspeech_to_text, flutter_tts, google_speech
Image recognitiontflite_flutter, google_mlkit_image_labeling
Face detectiongoogle_mlkit_face_detection
Language translationgoogle_mlkit_translation
Sentiment analysisUse on-device or API model

5. Security Tips

  • Use environment variables for API keys (e.g., flutter_dotenv)
  • Avoid hardcoding keys in the app
  • Use backend servers as proxies for secure API interaction if needed

6. Real-World Example Projects

  • AI Chatbot App with OpenAI + Flutter
  • Image Classifier using TensorFlow Lite
  • Voice Assistant using speech-to-text + AI model
  • Document Scanner with text recognition

Final Notes

  • For quick AI integration, cloud APIs are easier.
  • For offline capability, use tflite_flutter.
  • Always optimize models for mobile using quantization or model compression.

Related posts

What is bloc and how to use in flutter application step by step guide

Dheeraj Pal

How to make Diary App using Flutter and Getx

Dheeraj Pal

How to use ListView.builder in flutter

Dheeraj Pal

Leave a Comment