flutterdartgoogle-gemini

Unhandled Exception: **GeminiException** => The connection errored:


I'm using flutter_gemini package in my project. Yesterday it worked for me, I could chat with the Gemini AI, but today I can't. It is strange, because I did not touch the code. I receive this error:

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: GeminiException => The connection errored: Failed host lookup: 'generativelanguage.googleapis.com' This indicates an error which most likely cannot be solved by the library.

This is how the Gemini is initialized:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
  ]);
  Gemini.init(apiKey: kGeminiApiKey);
  runApp(const MyApp());

And this is how I use it in my StatefulWidget:

 gemini.streamGenerateContent(question, images: images).listen((event) {
        // logic goes here
      });

Why wont this work for me?


Solution

  • This error is due to Flutter's release mode not putting the INTERNET permission in the Android manifest. So, you have to do that manually.

    Add this permission to your android/app/src/main/AndroidManifest.xml file

    <uses-permission android:name="android.permission.INTERNET" />