gpt4 book ai didi

debugging - Dialogflow Flutter Chatbot - NoSuchMethodError : The method '[]' was called on null

转载 作者:行者123 更新时间:2023-12-03 02:42:50 25 4
gpt4 key购买 nike

我有以下问题:
NoSuchMethodError: 方法 '[]' 在 null 上被调用
当我在聊天机器人上发送消息时。

我试图学习如何从媒体创建聊天机器人:
https://medium.com/flutter-community/build-a-chatbot-in-20-minutes-using-flutter-and-dialogflow-8e9af1014463

运行正常的代码如下:

import 'package:flutter/material.dart';
import 'package:flutter_dialogflow/dialogflow_v2.dart';

class HomePageDialogflow extends StatefulWidget {
HomePageDialogflow({Key key, this.title}) : super(key: key);

final String title;

@override
_HomePageDialogflow createState() => new _HomePageDialogflow();
}

class _HomePageDialogflow extends State<HomePageDialogflow> {
final List<ChatMessage> _messages = <ChatMessage>[];
final TextEditingController _textController = new TextEditingController();

Widget _buildTextComposer() {
return new IconTheme(
data: new IconThemeData(color: Theme.of(context).accentColor),
child: new Container(
margin: const EdgeInsets.symmetric(horizontal: 8.0),
child: new Row(
children: <Widget>[
new Flexible(
child: new TextField(
controller: _textController,
onSubmitted: _handleSubmitted,
decoration:
new InputDecoration.collapsed(hintText: "Send a message"),
),
),
new Container(
margin: new EdgeInsets.symmetric(horizontal: 4.0),
child: new IconButton(
icon: new Icon(Icons.send),
onPressed: () => _handleSubmitted(_textController.text)),
),
],
),
),
);
}

void Response(query) async {
_textController.clear();
AuthGoogle authGoogle =
await AuthGoogle(fileJson: "assets/comply-267617-22528f20809f.json")
.build();
Dialogflow dialogflow =
Dialogflow(authGoogle: authGoogle, language: Language.english);
AIResponse response = await dialogflow.detectIntent(query);
ChatMessage message = new ChatMessage(
text: response.getMessage() ??
new CardDialogflow(response.getListMessage()[0]).title,
name: "Bot",
type: false,
);
setState(() {
_messages.insert(0, message);
});
}

void _handleSubmitted(String text) {
_textController.clear();
ChatMessage message = new ChatMessage(
text: text,
name: "Promise",
type: true,
);
setState(() {
_messages.insert(0, message);
});
Response(text);
}

@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
leading: new IconButton(
icon: new Icon(Icons.arrow_back),
onPressed: (){Navigator.pop(context);}),
centerTitle: true,
title: new Text("COMPLYNION"),
),
body: new Column(children: <Widget>[
new Flexible(
child: new ListView.builder(
padding: new EdgeInsets.all(8.0),
reverse: true,
itemBuilder: (_, int index) => _messages[index],
itemCount: _messages.length,
)),
new Divider(height: 1.0),
new Container(
decoration: new 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>[
new Container(
margin: const EdgeInsets.only(right: 16.0),
child: new CircleAvatar(child: new Text('B')),
),
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Text(this.name,
style: new TextStyle(fontWeight: FontWeight.bold)),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Text(text),
),
],
),
),
];
}

List<Widget> myMessage(context) {
return <Widget>[
new Expanded(
child: new Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: <Widget>[
new Text(this.name, style: Theme.of(context).textTheme.subhead),
new Container(
margin: const EdgeInsets.only(top: 5.0),
child: new Text(text),
),
],
),
),
new Container(
margin: const EdgeInsets.only(left: 16.0),
child: new CircleAvatar(
child: new Text(
this.name[0],
style: new TextStyle(fontWeight: FontWeight.bold),
)),
),
];
}

@override
Widget build(BuildContext context) {
return new Container(
margin: const EdgeInsets.symmetric(vertical: 10.0),
child: new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: this.type ? myMessage(context) : otherMessage(context),
),
);
}
}


在第 51 行,代码 AIResponse response = await dialogflow.detectIntent(query);尽管我已经在上面的行中初始化了对话流,但给了我一个错误。

最佳答案

我以前遇到过同样的问题,然后我尝试使用此角色创建一个新凭据:

Dialogflow -> Dialogflow API 管理员。

这肯定会起作用。

关于debugging - Dialogflow Flutter Chatbot - NoSuchMethodError : The method '[]' was called on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60255609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com