【问题标题】:NoSuchMethodError (NoSuchMethodError: The method '[]' was called on nullNoSuchMethodError (NoSuchMethodError: 方法 '[]' 在 null 上被调用
【发布时间】:2020-07-13 23:51:17
【问题描述】:

我尝试将 Dialogflow API 集成到我的颤振应用程序中;提交消息后,我应该得到 Dialogflow 的回复,但是 我收到一个错误:

发生了异常。 NoSuchMethodError (NoSuchMethodError: 的 在 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();
  }
}

我是不是在某个地方弄错了?

谢谢

【问题讨论】:

  • 你从 AIResponse 中得到了什么?
  • 该错误意味着您的消息数组为空或为空。
  • 感谢您的回复,我已经收到了

标签: flutter dart dialogflow-es


【解决方案1】:

我相信这是你的问题。 void response(query) async 应该是 Future&lt;void&gt; 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);
  }

试试看

【讨论】:

  • 出了什么问题
  • 如果这是正确的答案,你应该接受这个答案,如果不是,你应该发布正确的答案并将其标记为这个网站是为了帮助他人:)
【解决方案2】:
  1. 转到您的 Dialogflow 代理设置。

  2. 单击服务帐户链接,这将打开您的 Google 项目服务帐户。

  3. 单击您的服务帐户 --> 添加密钥 --> 创建一个新密钥 --> JSON 并下载它。

  4. 在您的项目中导入 JSON 文件并使用以下语句插入链接:

    AuthGoogle authGoogle = await AuthGoogle(fileJson: "path-to-your-json-file").build();

【讨论】:

    猜你喜欢
    • 2021-10-25
    • 2020-10-12
    • 2019-02-07
    • 2020-04-09
    • 2019-12-26
    • 2020-09-25
    • 1970-01-01
    • 2020-06-07
    • 2022-11-07
    相关资源
    最近更新 更多