I am trying to implement Smart Reply functionality in my flutter app using Google’s ML Kit.
But I am getting an error in this part of the code-
`void _generateSmartReplies() async {
final messageText = _messageController.text;
final timestamp = DateTime.now().millisecondsSinceEpoch;
// Add message to the conversation context
_smartReply.addMessageToConversationFromLocalUser(messageText, timestamp);
// Fetch smart replies based on the updated conversation context
final response = await _smartReply.suggestReplies();
setState(() {
_smartReplies = response.suggestions.map((suggestion) => suggestion.getText()).toList();
});
}`
The error is-
The method ‘getText’ isn’t defined for the type ‘String’.
Try correcting the name to the name of an existing method, or defining a method named ‘getText’.
Can someone please suggest me what to do ?
This is the entire code-
`
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ca/Addons/chat_bubble.dart';
import 'package:ca/models/auth_gate.dart';
import 'package:google_ml_kit/google_ml_kit.dart';
import 'package:ca/models/chatservice.dart';
import 'package:google_mlkit_smart_reply/google_mlkit_smart_reply.dart';
class ChatScreen extends StatefulWidget {
final String recieverEmail;
final String recieverId;
ChatScreen(
{super.key, required this.recieverEmail, required this.recieverId});
@override
State<ChatScreen> createState() => _ChatScreenState();
}
class _ChatScreenState extends State<ChatScreen> {
final TextEditingController _messageController = TextEditingController();
final Chatservice _chatService = Chatservice();
FocusNode myfocusNode = FocusNode();
final SmartReply _smartReply =SmartReply();
List<String> _smartReplies = [];
@override
void initState() {
super.initState();
myfocusNode.addListener(() {
if (myfocusNode.hasFocus) {
Future.delayed(
const Duration(milliseconds: 500),
() => scrollDown(),
);
}
});
Future.delayed(
const Duration(milliseconds: 500),
() => scrollDown(),
);
}
@override
void dispose() {
myfocusNode.dispose();
_messageController.dispose();
_smartReply.close();
super.dispose();
}
final ScrollController _scrollController = ScrollController();
void scrollDown() {
_scrollController.animateTo(
_scrollController.position.maxScrollExtent,
duration: const Duration(seconds: 1),
curve: Curves.fastOutSlowIn,
);
}
void _sendMessage() async {
if (_messageController.text.isNotEmpty) {
await _chatService.sendMessage(
widget.recieverId, _messageController.text);
_messageController.clear();
_generateSmartReplies();
}
scrollDown();
}
void _generateSmartReplies() async {
final messageText = _messageController.text;
final timestamp = DateTime.now().millisecondsSinceEpoch;
// Add message to the conversation context
_smartReply.addMessageToConversationFromLocalUser(messageText, timestamp);
// Fetch smart replies based on the updated conversation context
final response = await _smartReply.suggestReplies();
setState(() {
_smartReplies = response.suggestions.map((suggestion) => suggestion.getText()).toList();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
widget.recieverEmail,
style: Theme.of(context)
.textTheme
.titleLarge!
.copyWith(fontWeight: FontWeight.bold),
),
centerTitle: true,
elevation: 0,
actions: [
IconButton(
onPressed: () {
Get.changeThemeMode(
Get.isDarkMode ? ThemeMode.light : ThemeMode.dark);
},
icon: Icon(
Get.isDarkMode ? Icons.dark_mode : Icons.light_mode,
),
),
],
),
body: Column(
children: [
Expanded(
child: _buildMessageList(),
),
_buildSmartReplies(),
_buildUserInput(),
],
),
);
}
Widget _buildMessageList() {
String senderID = getCurrentUser()!.uid;
return StreamBuilder(
stream: _chatService.getMessages(widget.recieverId, senderID),
builder: (context, snapshot) {
if (snapshot.hasError) {
return const Text("error");
}
if (snapshot.connectionState == ConnectionState.waiting) {
return const Text("Loading");
}
return ListView(
controller: _scrollController,
children:
snapshot.data!.docs.map((doc) => _buildMessageItem(doc)).toList(),
);
},
);
}
Widget _buildMessageItem(DocumentSnapshot doc) {
Map<String, dynamic> data = doc.data() as Map<String, dynamic>;
bool isCurrentUser = data["senderID"] == getCurrentUser()!.uid;
var alignment =
isCurrentUser ? Alignment.centerRight : Alignment.centerLeft;
return Container(
alignment: alignment,
child: Column(
crossAxisAlignment:
isCurrentUser ? CrossAxisAlignment.end : CrossAxisAlignment.start,
children: [
ChatBubble(message: data["message"], iscurrentUser: isCurrentUser),
],
),
);
}
Widget _buildUserInput() {
return Row(
children: [
Expanded(
child: TextField(
controller: _messageController,
focusNode: myfocusNode,
decoration: InputDecoration(
hintText: 'Enter your message...',
hintStyle: const TextStyle(color: Colors.white),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: const BorderSide(color: Colors.white, width: 1.0),
),
contentPadding: const EdgeInsets.symmetric(horizontal: 16),
),
),
),
IconButton(
icon: const Icon(Icons.send),
onPressed: _sendMessage,
),
],
);
}
Widget _buildSmartReplies() {
return _smartReplies.isNotEmpty
? Container(
padding: const EdgeInsets.symmetric(vertical: 8.0),
color: Colors.grey[200],
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: _smartReplies.map((reply) {
return GestureDetector(
onTap: () {
_messageController.text = reply;
},
child: Container(
padding: const EdgeInsets.symmetric(
vertical: 6.0, horizontal: 12.0),
margin: const EdgeInsets.symmetric(horizontal: 4.0),
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(20.0),
),
child: Text(
reply,
style: TextStyle(color: Colors.white),
),
),
);
}).toList(),
),
),
)
: Container();
}
}