Firebase AI Logic
Connect a screen to Gemini through Firebase AI Logic, without an agent backend.
Firebase AI Logic lets a Flutter app call Gemini without a backend of your own. Firebase calls the model for the app, so the app ships no model API key, and App Check lets only your app make those calls. The tandem_firebase_ai package connects a Tandem screen to it. Gemini reads what the screen lists with . and proposes calls of its listed methods. Tandem checks the arguments, runs the methods in the app and asks the user to approve the calls that need approval.
tandem_firebase_ai 0.2 needs tandem 0.4. The package is outside the stable core, so its API can change in a minor release.
Set up Firebase
- Create a Firebase project in the Firebase console, or open one you have.
- In the project, open Firebase AI Logic and enable the Gemini Developer API. Firebase's get started guide walks through it.
- Register your app in the project for each platform it runs on, as the same guide shows. Firebase gives each registration its configuration: an API key, an app ID, a messaging sender ID and the project ID. These identify your Firebase app and are not a Gemini API key.
- Set up App Check. A debug build uses its debug provider: on the first run the SDK prints a debug token, which you register in the Firebase console. A release build uses Play Integrity on Android and App Attest with a DeviceCheck fallback on Apple platforms. A web build uses a reCAPTCHA v3 site key registered for the app's domain.
- Choose a Gemini model available in your project that supports function calling. doc/checks.json lists the models and Firebase backends each release ran on.
Every message makes a real model request that Firebase may bill. Set quotas for the project in the Firebase console.
Connect the screen
Add the packages:
flutter pub add tandem_firebase_ai firebase_ai firebase_core firebase_app_checkBefore the first message, initialize Firebase with your app's configuration and activate App Check. The example's firebase_setup. does both.
Create the connection once, outside build:
final connection = FirebaseConnection(
firebase: FirebaseAI.googleAI(),
model: model,
instructions:
'Help the user prepare their order. '
'Describe the result after each change. '
'Only place an order when the user asks you to.',
);Import package:firebase_ai/ and package:tandem_firebase_ai/. model is the name of the model you chose. instructions go into the model's system instruction.
Pass the connection to TandemScope.:
child: TandemScope.connected(
surface: (context) => context.read<OrderCubit>(),
connection: connection,
approvals: const .fromState(),The screen and the chat go in the scope's child, as with an agent backend. Several screens can share one connection, and each keeps its own conversation. The connection needs no dispose call.
Run the example
The example's order form runs on Firebase AI Logic. Put your Firebase app's configuration into a JSON file outside the repository:
{
"FIREBASE_API_KEY": "your Firebase app API key",
"FIREBASE_APP_ID": "your Firebase app ID",
"FIREBASE_MESSAGING_SENDER_ID": "your messaging sender ID",
"FIREBASE_PROJECT_ID": "your project ID",
"FIREBASE_MODEL": "your Gemini model name",
"FIREBASE_RECAPTCHA_SITE_KEY": "your web site key, omitted for native"
}Register a Firebase app whose package or bundle ID matches the example. Then, from example, run:
fvm flutter run -t lib/main_firebase.dart --dart-define-from-file=/absolute/path/firebase-example.jsonTap the suggested message. The model adds the monitors, then asks for approval to place the order. The example keeps its orders in memory and places no real order.
What a run does
- A run sends the screen's current
.values and its listed methods, and asks Firebase for one answer.sees - Text reaches the chat while Firebase streams it. Calls of the methods reach Tandem once Firebase has sent the whole answer.
- A
TandemViewcatalog works through thetandem_show_viewtool, so the model can draw views. - Stop ends the run in the app and aborts its request. It does not undo a method that already ran, and Firebase may go on generating and charge for it.
- A failed run reaches
onEventasRunFailedwith aFirebaseErrorCode.idleTimeout, 120 seconds by default, fails a run whose model sends nothing for that long. FirebaseErrorCode.means the thread holds calls or messages this connection cannot send. Start a new thread. Keep a thread with the connection that started it, also when you change the model.incompatibleHistory
The connection does not support files, images, audio, the Live API or grounding. Firebase Authentication and quotas stay in your Firebase configuration. An approval card is not authorization: the service that stores protected data still decides who may change it.
The adapter's README has the settings and every error code.