Debugging Dialogflow颤振聊天机器人-NoSuchMethodError:方法';[]和#x27;被调用为空

Debugging Dialogflow颤振聊天机器人-NoSuchMethodError:方法';[]和#x27;被调用为空,debugging,flutter,dart,Debugging,Flutter,Dart,我有以下问题: NoSuchMethodError:对null调用了方法“[]” 当我在聊天机器人上发送消息时 我试图学习如何从媒体创建聊天机器人: 运行正常的代码如下所示: import 'package:flutter/material.dart'; import 'package:flutter_dialogflow/dialogflow_v2.dart'; class HomePageDialogflow extends StatefulWidget { HomePageDial

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

我试图学习如何从媒体创建聊天机器人:

运行正常的代码如下所示:

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),
      ),
    );
  }
}

导入“包装:颤振/材料.省道”;
导入“包:颤振_对话框流/对话框流_v2.dart”;
类HomePageDialogflow扩展StatefulWidget{
HomePageDialogflow({Key-Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_HomePageDialogflow createState()=>new_HomePageDialogflow();
}
类_HomePageDialogflow扩展状态{
最终列表_消息=[];
最终文本编辑控制器_textController=新文本编辑控制器();
小部件_buildTextComposer(){
返回新的IconTheme(
数据:新的IconThemeData(颜色:Theme.of(context.accentColor)),
子容器:新容器(
边距:常量边集。对称(水平:8.0),
孩子:新的一排(
儿童:[
新柔性(
孩子:新文本字段(
控制器:_textController,
未提交:_handleSubmitted,
装饰:
新建InputDecoration.collapsed(hintText:“发送消息”),
),
),
新容器(
边距:新边集。对称(水平:4.0),
孩子:新的图标按钮(
图标:新图标(Icons.send),
按下时:()=>_handleSubmitted(_textController.text)),
),
],
),
),
);
}
无效响应(查询)异步{
_textController.clear();
AuthGoogle AuthGoogle=
等待AuthGoogle(fileJson:“assets/compliance-267617-22528f2089f.json”)
.build();
对话流对话流=
Dialogflow(authGoogle:authGoogle,语言:language.english);
AIResponse response=wait dialogflow.detectinent(查询);
ChatMessage message=新建ChatMessage(
文本:response.getMessage()??
新建CardDialogflow(response.getListMessage()[0])。标题,
名称:“机器人”,
类型:false,
);
设置状态(){
_消息。插入(0,消息);
});
}
void\u handleSubmitted(字符串文本){
_textController.clear();
ChatMessage message=新建ChatMessage(
文本:文本,
名称:“承诺”,
类型:对,
);
设置状态(){
_消息。插入(0,消息);
});
答复(案文);
}
@凌驾
小部件构建(构建上下文){
归还新脚手架(
appBar:新的appBar(
领先:新图标按钮(
图标:新图标(图标。箭头返回),
onPressed:(){Navigator.pop(context);}),
标题:对,
标题:新文本(“公司”),
),
正文:新列(子项:[
新柔性(
子项:新建ListView.builder(
填充:新边缘设置。全部(8.0),
相反:是的,
itemBuilder:(\u,int index)=>\u消息[索引],
itemCount:_messages.length,
)),
新隔板(高度:1.0),
新容器(
装饰:新盒子装饰(颜色:主题.of(上下文).cardColor),
子项:_buildTextComposer(),
),
]),
);
}
}
类ChatMessage扩展了无状态小部件{
ChatMessage({this.text,this.name,this.type});
最终字符串文本;
最后的字符串名;
最终布尔型;
列出其他消息(上下文){
返回[
新容器(
边距:仅限常量边集(右:16.0),
child:new CircleAvatar(child:new Text('B')),
),
新扩展(
子:新列(
crossAxisAlignment:crossAxisAlignment.start,
儿童:[
新文本(this.name,
样式:新文本样式(fontWeight:fontWeight.bold)),
新容器(
边距:仅限常量边集(顶部:5.0),
子:新文本(文本),
),
],
),
),
];
}
列表myMessage(上下文){
返回[
新扩展(
子:新列(
crossAxisAlignment:crossAxisAlignment.end,
儿童:[
新文本(this.name,style:Theme.of(context.textTheme.subhead),
新容器(
边距:仅限常量边集(顶部:5.0),
子:新文本(文本),
),
],
),
),
新容器(
边距:仅限常量边集(左:16.0),
孩子:新CircleAvatar(
儿童:新文本(
此.name[0],
样式:新文本样式(fontWeight:fontWeight.bold),
)),
),
];
}
@凌驾
小部件构建(构建上下文){
退回新货柜(
边距:常数边集。对称(垂直:10.0),
孩子:新的一排(
crossAxisAlignment:crossAxisAlignment.start,
子项:this.type?myMessage(上下文):otherMessage(上下文),
),
);
}
}

在第51行,代码AIResponse response=wait dialogflow.detectinent(查询);尽管我已经在上面的行中初始化了dialogflow,但给了我一个错误。

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

Dialogflow->Dialogflow API管理。


这肯定会奏效。

确保您的google云控制台项目与Dialog Flow项目匹配。 您的对话框流项目应该有一个项目Id,您可以在google console项目下拉列表中找到它


创建此项目的API凭据并确保已启用计费。

我很困惑,我仍然有问题,我导出密钥服务与管理员和dialogflow集成在任何情况下仍然有错误异常:NoSuchMethodError:我创建的bot和默认bot的方法“[]”被调用为null。。。