From 82ffbd5190705137ed2cee8f41da8b33f193dc71 Mon Sep 17 00:00:00 2001 From: aniket866 Date: Sun, 8 Feb 2026 00:10:15 +0530 Subject: [PATCH 1/2] try-catch-block --- lib/services/ai_service.dart | 65 ++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/lib/services/ai_service.dart b/lib/services/ai_service.dart index 151cfc3..d0a1754 100644 --- a/lib/services/ai_service.dart +++ b/lib/services/ai_service.dart @@ -301,7 +301,7 @@ class AIService { } } - // Add system message as the first message with role "model" + // Add system message contents.add({ "role": "model", "parts": [ @@ -389,40 +389,49 @@ class AIService { ); if (response.statusCode == 200) { - final responseData = jsonDecode(response.body); - - // Check if the response contains a function call - final candidates = responseData['candidates'] as List; - if (candidates.isNotEmpty) { - final content = candidates[0]['content']; - final parts = content['parts'] as List; + try { + // FIX: Added try-catch block for safe JSON decoding + final responseData = jsonDecode(response.body); - for (var part in parts) { - if (part.containsKey('functionCall')) { - final functionCall = part['functionCall']; - final functionName = functionCall['name']; - final arguments = functionCall['args']; - - return { - 'type': 'function_call', - 'function_name': functionName, - 'arguments': arguments, - 'raw_response': jsonEncode(responseData), - }; + // Check if the response contains a function call + final candidates = responseData['candidates'] as List; + if (candidates.isNotEmpty) { + final content = candidates[0]['content']; + final parts = content['parts'] as List; + + for (var part in parts) { + if (part.containsKey('functionCall')) { + final functionCall = part['functionCall']; + final functionName = functionCall['name']; + final arguments = functionCall['args']; + + return { + 'type': 'function_call', + 'function_name': functionName, + 'arguments': arguments, + 'raw_response': jsonEncode(responseData), + }; + } } + + // If no function call is detected, return as regular message + return { + 'type': 'message', + 'content': candidates[0]['content']['parts'][0]['text'] ?? '', + }; } - // If no function call is detected, return as regular message return { - 'type': 'message', - 'content': candidates[0]['content']['parts'][0]['text'] ?? '', + 'type': 'error', + 'content': 'No response generated', + }; + } catch (e) { + debugPrint('Error parsing AI response: $e'); + return { + 'type': 'error', + 'content': 'Sorry, I received an invalid response from the AI server.', }; } - - return { - 'type': 'error', - 'content': 'No response generated', - }; } else { debugPrint('Error from Gemini API: ${response.statusCode} ${response.body}'); return { From a2f80aa12806946d14f00df98325aef581a0ab38 Mon Sep 17 00:00:00 2001 From: Aniket Date: Sun, 8 Feb 2026 00:29:37 +0530 Subject: [PATCH 2/2] Code rabbit follow up Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- lib/services/ai_service.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/services/ai_service.dart b/lib/services/ai_service.dart index d0a1754..2a4b8e1 100644 --- a/lib/services/ai_service.dart +++ b/lib/services/ai_service.dart @@ -413,11 +413,10 @@ class AIService { }; } } - // If no function call is detected, return as regular message return { 'type': 'message', - 'content': candidates[0]['content']['parts'][0]['text'] ?? '', + 'content': parts[0]['text'] ?? '', }; }