0

I'm trying to send an image and text to Gemini using the firebase_ai package in Flutter, but I'm getting a persistent error: The method 'DataPart' isn't defined for the type 'AiAssistantService'.

The strange thing is that I am using the official code structure from the examples.

Here is my function:

import 'dart:typed_data';
import 'package:firebase_ai/firebase_ai.dart';
// ... other imports

Future<void> getResponseForImage(String userInput, Uint8List imageBytes) async {
  try {
    final googleAI = FirebaseAI.googleAI();

    final model = googleAI.generativeModel(
      model: 'gemini-1.5-flash-latest',
    );

    // According to docs, this should be the correct way to structure the content
    final content = [
        TextPart("Some text prompt"),
        DataPart('image/jpeg', imageBytes), // <-- The error happens here
    ];

    final response = await model.generateContent(content);
    print(response.text);

  } catch (e) {
    print("An error occurred: $e");
  }
}

I was expecting the code to work correctly without any errors. Instead, I always get the error: 'DataPart' isn't defined.

I have already tried many things to fix this:

  • I ran 'flutter clean'.
  • I deleted the pubspec.lock file and ran 'flutter pub get'.
  • I ran 'flutter pub cache repair'.
  • I restarted VS Code many times.

None of these solutions worked.

3
  • Hello, please link the documentation you are following Commented Oct 3 at 18:12
  • Hello, thank you for your comment and my apologies for the confusion. This is the CORRECT official package page that I am following on pub.dev: pub.dev/packages/firebase_ai The code example under the "Multi-modal input" section on that page shows the exact structure that I am using, but I still get the DataPart is not defined error. Commented Oct 3 at 18:32
  • 1
    I don't seem to be able to find that on that page. Rosario's suggestion the other hand seems like what you might be looking for. Commented Oct 6 at 12:54

1 Answer 1

2

I can't find the documentation that you claim to see in pub.dev, but the official Firebase documentation suggests that you should use InlineDataPart.

So your code would become:

Future<void> getResponseForImage(String userInput, Uint8List imageBytes) async {
  try {
    final googleAI = FirebaseAI.googleAI();

    final model = googleAI.generativeModel(
      model: 'gemini-2.5-flash',
    );

    // Note the use of Content.multi():
    final content = Content.multi([
        TextPart("Some text prompt"),
        InlineDataPart('image/jpeg', imageBytes)
    ]);

    final response = await model.generateContent([content]);
    print(response.text);

  } catch (e) {
    print("An error occurred: $e");
  }
}

Also note that I changed the model name from gemini-1.5-flash-latest to gemini-2.5-flash because the Gemini 1.5 models were turned down last month.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.