Skip to content

Fix: ChatBot not displaying Gemini responses + Security improvement (.env setup) #424#427

Merged
andoriyaprashant merged 1 commit into
andoriyaprashant:mainfrom
riddhimagupta2:fix/chatbot-error-handling
May 15, 2026
Merged

Fix: ChatBot not displaying Gemini responses + Security improvement (.env setup) #424#427
andoriyaprashant merged 1 commit into
andoriyaprashant:mainfrom
riddhimagupta2:fix/chatbot-error-handling

Conversation

@riddhimagupta2

@riddhimagupta2 riddhimagupta2 commented May 15, 2026

Copy link
Copy Markdown
Contributor

🐛 424 - ChatBot Bug Fix + API Key Security Improvement


Describe Problem / Root Cause

1. Chatbot Issue

  • The chatbot was successfully accepting user input.
  • However, Gemini API responses were not being displayed in the UI.

Identified issues:

  • API response was sometimes null
  • Errors were not shown in the UI
  • No proper state handling for failed or empty responses
  • No feedback when API call failed

➡️ This caused a broken chat experience where only user messages appeared and bot responses were missing.


2. Security Issue

  • Gemini API key was previously hardcoded in the source code.
  • This exposed sensitive credentials in the repository.
  • It also made environment configuration less flexible.

Solution Proposed

ChatBot Fix

  • Wrapped Gemini API call inside try-catch to handle runtime errors safely
  • Added null-safe handling for API response:
response.text ?? "No response received"
  • Ensured bot response is always added to _messages using setState
  • Added error logging for debugging:
print("Gemini Error: $e");
  • Ensured UI updates correctly after every API response or failure

Security Fix (dotenv implementation)

  • Removed hardcoded API key from source code
  • Added flutter_dotenv dependency
  • Created .env file for secure API storage
  • Updated main.dart to load environment variables:
await dotenv.load(fileName: ".env");
  • Replaced API key usage with:
dotenv.env['API_KEY'] ?? ''

Security Improvement (IMPORTANT)

  • Added .env file to .gitignore to prevent sensitive data from being pushed to GitHub
.env

Additional Improvements

  • Improved chatbot reliability and stability
  • Better error visibility during API failures
  • Prevents silent failures (no response cases are now visible in UI)
  • Ensures consistent chat flow between user and bot
  • Improved project security and production readiness

🧪 Testing Done

  • Chatbot responses now appear correctly
  • API errors are visible instead of silent failure
  • No crashes or null response issues
  • .env configuration working properly
  • .env file is ignored from Git tracking
  • App runs successfully with secure API handling

Screenshots

Before Fix (Bug)

  • User message appears

  • Bot response missing or blank

    Add here:

Screenshot 2026-05-15 130859

After Fix (Working + Secure)

  • Bot responds correctly

  • Messages display properly

  • API key removed from source code

  • .env working successfully

    Add here:

Screenshot 2026-05-15 195619 ---

Notes

  • .env file is added to .gitignore to ensure security

@netlify

netlify Bot commented May 15, 2026

Copy link
Copy Markdown

Deploy Preview for aquamarine-kheer-83feda failed. Why did it fail? →

Name Link
🔨 Latest commit 3185a77
🔍 Latest deploy log https://app.netlify.com/projects/aquamarine-kheer-83feda/deploys/6a0735ea94f23800087e2002

@github-actions

Copy link
Copy Markdown

Thank you for submitting your pull request! We'll review it as soon as possible.

@riddhimagupta2

Copy link
Copy Markdown
Contributor Author

The Netlify check is failing because "flutter build apk" is being used in deployment, but Flutter doesn’t seem to be available in the Netlify environment. My changes are related only to chatbot error handling and not deployment configuration.

@andoriyaprashant andoriyaprashant merged commit f9b52df into andoriyaprashant:main May 15, 2026
1 of 5 checks passed
@riddhimagupta2

Copy link
Copy Markdown
Contributor Author

Hi @andoriyaprashant , my PR has the gssoc26 and difficulty labels but it is still not showing in my contributions after ~15 hours. Could you please check if the required labels (gssoc:approved etc.) are correctly added and synced for the leaderboard?

@andoriyaprashant

Copy link
Copy Markdown
Owner

Hi @andoriyaprashant , my PR has the gssoc26 and difficulty labels but it is still not showing in my contributions after ~15 hours. Could you please check if the required labels (gssoc:approved etc.) are correctly added and synced for the leaderboard?

Done @riddhimagupta2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants