Skip to content

Commit 3185a77

Browse files
Improve chatbot error handling and null response safety
1 parent 85ee113 commit 3185a77

9 files changed

Lines changed: 104 additions & 66 deletions

File tree

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
API_KEY=your_actual_gemini_api_key_here
0 Bytes
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
#Wed Dec 18 18:40:54 IST 2024
2-
gradle.version=8.8
1+
#Fri May 15 12:45:26 IST 2026
2+
gradle.version=9.0-milestone-1

android/app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ android {
4545
applicationId "com.example.opso"
4646
// You can update the following values to match your application needs.
4747
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
48-
minSdkVersion 23
48+
minSdkVersion flutter.minSdkVersion
4949
targetSdkVersion 34
5050
versionCode flutterVersionCode.toInteger()
5151
versionName flutterVersionName

lib/ChatBotPage.dart

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'package:adaptive_theme/adaptive_theme.dart';
22
import 'package:flutter/material.dart';
33
import 'package:google_generative_ai/google_generative_ai.dart';
4+
import 'package:flutter_dotenv/flutter_dotenv.dart';
45

5-
// Replace with your actual Gemini API key
6-
const apiKey = 'api-key';
6+
final String apiKey = dotenv.env['API_KEY'] ?? '';
77

88
class ChatBotPage extends StatefulWidget {
99
const ChatBotPage({Key? key}) : super(key: key);
@@ -23,28 +23,46 @@ class _ChatBotPageState extends State<ChatBotPage> {
2323
_initializeModel();
2424
}
2525

26-
void _initializeModel() async {
26+
void _initializeModel() {
27+
2728
_model = GenerativeModel(
28-
model: 'gemini-1.5-flash-latest',
29+
model: 'gemini-2.5-flash',
2930
apiKey: apiKey,
3031
);
3132
}
3233

3334
void _sendMessage() async {
34-
if (_controller.text.isEmpty) return;
35+
if (_controller.text.trim().isEmpty) return;
36+
37+
final userMessage = _controller.text.trim();
3538

3639
setState(() {
37-
_messages.add({"sender": "user", "text": _controller.text});
40+
_messages.add({
41+
"sender": "user",
42+
"text": userMessage,
43+
});
3844
});
3945

40-
final prompt = _controller.text + " don't give answer in markdown format";
4146
_controller.clear();
47+
try {
48+
if (_model == null) return;
49+
50+
final response = await _model!.generateContent([
51+
Content.text(userMessage),
52+
]);
4253

43-
if (_model != null) {
44-
final content = [Content.text(prompt)];
45-
final response = await _model!.generateContent(content);
4654
setState(() {
47-
_messages.add({"sender": "bot", "text": response.text!});
55+
_messages.add({
56+
"sender": "bot",
57+
"text": response.text ?? "No response",
58+
});
59+
});
60+
} catch (e) {
61+
setState(() {
62+
_messages.add({
63+
"sender": "bot",
64+
"text": "Error: $e",
65+
});
4866
});
4967
}
5068
}
@@ -53,9 +71,8 @@ class _ChatBotPageState extends State<ChatBotPage> {
5371
Widget build(BuildContext context) {
5472
return Scaffold(
5573
appBar: AppBar(
56-
leading: IconButton(
74+
leading: IconButton(
5775
icon: const Icon(Icons.arrow_back_ios),
58-
5976
onPressed: () => Navigator.of(context).pop(),
6077
),
6178
centerTitle: true,
@@ -70,23 +87,30 @@ class _ChatBotPageState extends State<ChatBotPage> {
7087
final message = _messages[index];
7188
final isUser = message["sender"] == "user";
7289
return Align(
73-
alignment: isUser ? Alignment.centerRight : Alignment.centerLeft,
90+
alignment:
91+
isUser ? Alignment.centerRight : Alignment.centerLeft,
7492
child: Container(
7593
constraints: BoxConstraints(
7694
maxWidth: MediaQuery.of(context).size.width * 0.75,
7795
),
78-
margin: const EdgeInsets.symmetric(vertical: 5.0, horizontal: 10.0),
96+
margin: const EdgeInsets.symmetric(
97+
vertical: 5.0, horizontal: 10.0),
7998
padding: const EdgeInsets.all(10.0),
8099
decoration: BoxDecoration(
81100
color: isUser
82-
? (AdaptiveTheme.of(context).mode.isDark ? Color.fromARGB(255, 31, 49, 70) : Colors.blue[100])
83-
: (AdaptiveTheme.of(context).mode.isDark ? Colors.grey[700] : Colors.grey[300]),
101+
? (AdaptiveTheme.of(context).mode.isDark
102+
? Color.fromARGB(255, 31, 49, 70)
103+
: Colors.blue[100])
104+
: (AdaptiveTheme.of(context).mode.isDark
105+
? Colors.grey[700]
106+
: Colors.grey[300]),
84107
borderRadius: BorderRadius.circular(10.0),
85108
),
86109
child: Row(
87110
crossAxisAlignment: CrossAxisAlignment.start,
88111
children: [
89-
if (!isUser) const CircleAvatar(child: Icon(Icons.android)),
112+
if (!isUser)
113+
const CircleAvatar(child: Icon(Icons.android)),
90114
if (!isUser) const SizedBox(width: 5.0),
91115
Expanded(
92116
child: Column(
@@ -97,7 +121,8 @@ class _ChatBotPageState extends State<ChatBotPage> {
97121
),
98122
),
99123
if (isUser) const SizedBox(width: 5.0),
100-
if (isUser) const CircleAvatar(child: Icon(Icons.person)),
124+
if (isUser)
125+
const CircleAvatar(child: Icon(Icons.person)),
101126
],
102127
),
103128
),
@@ -130,17 +155,15 @@ class _ChatBotPageState extends State<ChatBotPage> {
130155
);
131156
}
132157

133-
134158
@override
135159
void dispose() {
136160
_controller.dispose();
137161
super.dispose();
138162
}
139163
}
140164

141-
142165
void main() {
143166
runApp(const MaterialApp(
144167
home: ChatBotPage(),
145168
));
146-
}
169+
}

lib/main.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_dotenv/flutter_dotenv.dart';
23
import 'package:flutter_screenutil/flutter_screenutil.dart';
34
import 'package:opso/programs%20screen/fossasia.dart';
45
import 'package:opso/programs%20screen/girl_script.dart';
@@ -22,6 +23,9 @@ import 'splash_screen.dart';
2223

2324
void main() async {
2425
WidgetsFlutterBinding.ensureInitialized();
26+
await dotenv.load(fileName: ".env");
27+
28+
2529
final savedThemeMode = await AdaptiveTheme.getThemeMode();
2630
await NotificationService.initialNotification();
2731
runApp(OpSoApp(savedThemeMode: savedThemeMode));

local.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
# Location of the SDK. This is only used by Gradle.
55
# For customization when using a Version Control System, please read the
66
# header note.
7-
#Sat Jun 08 20:37:50 IST 2024
8-
sdk.dir=E\:\\STUDY\\ANDROID_SDK\\Sdk
7+
#Fri May 15 12:44:42 IST 2026
8+
sdk.dir=C\:\\Users\\VICTUS\\AppData\\Local\\Android\\Sdk

0 commit comments

Comments
 (0)