我试图在Flutter应用中整合Dialogflow API;在我提交了消息之后,我应该从Dialogflow得到答复,但是 我收到错误消息:
发生异常。 NoSuchMethodError(NoSuchMethodError:The 方法'[]'在null上调用。
接收者:null已尝试 打电话:)
这是我的代码(chatbot.dart):
class Chatbot extends StatefulWidget {
Chatbot({
Key key,
this.title,
}) : super(key: key);
final String title;
@override
_HomePageDialogflowV2 createState() => _HomePageDialogflowV2();
}
class _HomePageDialogflowV2 extends State<Chatbot> {
final List<ChatMessage> _messages = <ChatMessage>[];
final TextEditingController _textController = TextEditingController();
Widget _buildTextComposer() {
return IconTheme(
data: IconThemeData(color: Theme.of(context).accentColor),
child: Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: Row(
children: <Widget>[
Flexible(
child: TextField(
controller: _textController,
onSubmitted: _handleSubmitted,
decoration:
InputDecoration.collapsed(hintText: "Send a message"),
),
),
Container(
margin: EdgeInsets.symmetric(horizontal: 4.0),
child: IconButton(
icon: Icon(Icons.send),
onPressed: () => _handleSubmitted(_textController.text),
),
),
],
),
),
);
}
void response(query) async {
_textController.clear();
AuthGoogle authGoogle = await AuthGoogle(
fileJson: "assets/credentials-assem.json",
).build();
Dialogflow dialogflow = Dialogflow(
authGoogle: authGoogle,
language: Language.ENGLISH,
);
AIResponse response = await dialogflow.detectIntent(query);
ChatMessage message = ChatMessage(
text: response.getMessage() ??
CardDialogflow(
response.getListMessage()[0],
).title,
name: "Bot",
type: false,
);
setState(() {
_messages.insert(0, message);
});
}
void _handleSubmitted(String text) {
_textController.clear();
ChatMessage message = ChatMessage(
text: text,
name: "Aseem",
type: true,
);
setState(() {
_messages.insert(0, message);
});
response(text);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("ChatBot"),
),
body: Column(children: <Widget>[
Flexible(
child: ListView.builder(
padding: EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, int index) => _messages[index],
itemCount: _messages.length,
)),
Divider(height: 1.0),
Container(
decoration: BoxDecoration(color: Theme.of(context).cardColor),
child: _buildTextComposer(),
),
]),
);
}
}
class ChatMessage extends StatelessWidget {
ChatMessage({
this.text,
this.name,
this.type,
});
final String text;
final String name;
final bool type;
List<Widget> otherMessage(context) {
return <Widget>[
Container(
margin: const EdgeInsets.only(right: 16.0),
child: CircleAvatar(
child: Image.asset("images/bot.png"),
),
),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
this.name,
style: TextStyle(fontWeight: FontWeight.bold),
),
Container(
margin: const EdgeInsets.only(top: 5.0),
child: Text(text),
),
],
),
),
];
}
List<Widget> myMessage(context) {
return <Widget>[
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
Text(this.name, style: Theme.of(context).textTheme.subhead),
Container(
margin: const EdgeInsets.only(top: 5.0),
child: Text(text),
),
],
),
),
Container(
margin: const EdgeInsets.only(left: 16.0),
child: CircleAvatar(
child: Text(this.name[0]),
),
),
];
}
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: this.type ? myMessage(context) : otherMessage(context),
),
);
}
}
Main.dart:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Chatbot();
}
}
我在某个地方犯了错误吗?
谢谢
答案 0 :(得分:0)
我相信这是您的问题。 void response(query) async
应该为Future<void> response(query) async
,并且当您调用响应函数时,您需要等待它,因此您必须使该函数以异步方式调用响应。
void _handleSubmitted(String text) async {
_textController.clear();
ChatMessage message = ChatMessage(
text: text,
name: "Aseem",
type: true,
);
setState(() {
_messages.insert(0, message);
});
await response(text);
}
尝试一下
答案 1 :(得分:0)