Migrating Flutter Web Google AI to Firebase: Beyond the Docs - Development Diary
Migrating Flutter Web Google AI to Firebase: Beyond the Docs
Google AI is evolving at an incredible pace. In less than a year, I've seen Gemini progress from version 1.0 to 2.5. As you might know, ITDOGTICS has developed several applications using Gemini, and this rapid evolution brings maintenance challenges, particularly with model deprecation. The recent announcement of Gemini 1.5's discontinuation, for instance, has led to the deprecation of some Flutter packages, including the
google_generative_ai
package I've been using. The next version will leverage Firebase AI.While the syntax transition is relatively straightforward, a significant shift is the move away from user-based API keys to a centralized Firebase approach. This change introduces new security considerations.
In this blog post, I'll guide you through the migration process, helping you securely transition your project from the original google_generative_ai
package to Firebase AI.
This guide is for:
- Flutter Developers
- Anyone interested in building Google Generative AI projects
How to Migrate Your Project
The migration process is generally simple. Here's a step-by-step guide:
1. Set Up Firebase for Your Project
First, you'll need to set up Firebase for your Flutter project. For detailed documentation on this, you can refer to the official Firebase documentation.
2. Enable AI Logic and App Check
After enabling AI Logic in your Firebase project, you'll be prompted to enable App Check.
Do not type anything into the reCAPTCHA secret key field directly in the Firebase UI;
this UI is misleading. The site ID generated on the reCAPTCHA website will be used within your code, a detail not explicitly stated elsewhere.
3. Update Your Code Syntax
Most of your existing syntax will remain the same, with one key change:
Replace GenerativeModel.
with FirebaseAI.googleAI()
.
4. Initialize Firebase and App Check in main.dart
In your main.dart
file, ensure you perform the necessary initializations:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
await FirebaseAppCheck.instance.activate(
// Use the site ID generated from the reCAPTCHA website
webRecaptchaSiteKey: 'YOUR_RECAPTCHA_SITE_ID',
);
runApp(MyApp());
}
It's at this point that some developers encounter issues, specifically the "Invalid AppCheck Token" error, which can be perplexing.
5. The Solution: Provide AppCheck Instance to FirebaseAI.googleAI()
The key to resolving the "Invalid AppCheck Token" error, a detail not widely documented, is to provide the FirebaseAppCheck.instance
to your FirebaseAI.googleAI()
call.
Your code should look like this:
// When initializing your GenerativeModel or making calls
FirebaseAI.googleAI(appCheck: FirebaseAppCheck.instance);
By following these steps and providing the FirebaseAppCheck
instance, your application will successfully migrate to Firebase AI with enhanced security, ensuring a smooth and secure transition for your Generative AI projects.
Comments
Post a Comment