babakcode / flutter_gemini

Flutter Google Gemini SDK
https://pub.dev/packages/flutter_gemini
BSD 3-Clause "New" or "Revised" License
178 stars 53 forks source link

Outdated version of Dio lib #38

Open ChunhThanhDe opened 3 months ago

ChunhThanhDe commented 3 months ago

Error Description

Issue:

When using the API call:

await gemini.text(
  message.toString(),
  modelName: "models/gemini-1.5-flash",
);

with the post function utilizing Dio multiple times:

Below is the source code of the gemini_service.dart file

@override
Future<Response> post(
  String route, {
  required Map<String, Object>? data,
  GenerationConfig? generationConfig,
  List<SafetySetting>? safetySettings,
  bool isStreamResponse = false,
}) async {
  cancelToken ??= CancelToken();
  if (safetySettings != null || this.safetySettings != null) {
    final listSafetySettings = safetySettings ?? this.safetySettings ?? [];
    final items = [];
    for (final safetySetting in listSafetySettings) {
      items.add({
        'category': safetySetting.category.value,
        'threshold': safetySetting.threshold.value,
      });
    }
    data?['safetySettings'] = items;
  }

  if (generationConfig != null || this.generationConfig != null) {
    data?['generationConfig'] =
        generationConfig?.toJson() ?? this.generationConfig?.toJson() ?? {};
  }

  return handler(() => dio.post(
        route,
        data: jsonEncode(data),
        queryParameters: {'key': apiKey},
        options: Options(
            responseType:
                isStreamResponse == true ? ResponseType.stream : null),
        cancelToken: cancelToken,
      ));
}

I am not getting empty "" response from Gemini and when I add Try Catch and Throw code to source code it throws below error:

Error Message:

TypeError: Instance of '_JsonMap': type '_JsonMap' is not a subtype of type 'String'
handle gemini error: TypeError: null: type 'Null' is not a subtype of type 'String'
Error
dart:sdk_internal 12006:11                                                                                    throw_
dart:sdk_internal 23004:15                                                                                    _failedAsCheck
dart:sdk_internal 22990:14

Cause:

Through debugging and adding try-catch blocks to the Gemini Flutter code, I discovered that the issue is within the integrated Dio library in Gemini Flutter.

Solution:

The current version of Dio used by Gemini Flutter is outdated. I recommend upgrading to the latest version of Dio. I have tested this upgrade and found no impact on the current source code of Gemini Flutter. Update that when I use Gemini Flutter library with latest Dio version, so far I have not encountered the same error again.


ChunhThanhDe commented 3 months ago

Hi @babakcode as mentioned above, I have created a Pull #40 for this bug, thank you if you integrate it.