🔥 Deep Dive · Intermediate

Firebase AI Logic trong Flutter: Gemini không cần backend riêng

Bạn không phải lúc nào cũng cần proxy Node/Go để gọi LLM từ Flutter. Firebase AI Logic (firebase_ai) cung cấp client typed cho Gemini, sẵn Auth, App Check và quota.

Sơ đồ: Firebase AI Logic

Vì sao team chọn

  • Gọi multimodal phía client (ảnh → log dinh dưỡng có cấu trúc) không phải dựng infra.
  • Server Prompt Templates giữ system prompt và tool definition ngoài binary.
  • Cùng project Firebase bạn đã dùng cho Crashlytics/Auth.

Dáng client tối thiểu

import 'package:firebase_ai/firebase_ai.dart';

final model = FirebaseAI.googleAI().generativeModel(model: 'gemini-2.5-flash');
final response = await model.generateContent([
  Content.text('Mô tả screenshot UI này cho bug report.'),
  Content.data('image/png', pngBytes),
]);
print(response.text);

(API surface thay đổi theo version — ghim package và đọc docs hiện tại.)

Checklist bảo mật

  1. Bật App Check.
  2. Yêu cầu Auth cho model đắt.
  3. Server template cho prompt chứa business rule.
  4. Client không giữ provider key dài hạn.

Khi nào dùng Genkit

Nếu flow, tool và observability ở server, ưu tiên Genkit Dart. Firebase AI Logic mạnh khi trải nghiệm sản phẩm ở trên device và nhạy latency.

#Flutter#Firebase#Gemini#AI