【问题标题】:Class 'String' has no instance method 'tostring', Flutter“String”类没有实例方法“tostring”,Flutter
【发布时间】:2020-09-03 12:06:32
【问题描述】:

我正在尝试从我的聊天机器人生成响应(使用对话流)

void response(query) async {
    AuthGoogle authGoogle = await AuthGoogle(
        fileJson: "Assets/amigo-pyhyyy-e2d1db5e1ee9.json").build();
    Dialogflow dialogflow = await Dialogflow(
        authGoogle: authGoogle, language: Language.english);
    AIResponse aiResponse = await dialogflow.detectIntent(query);
    setState(() {
      messages.insert(0, {"data": 0,
        "message": aiResponse.getListMessage()[0]["text"]["text"][0].tostring()
      });
    });

我收到此错误: E/flutter (8166): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理异常:NoSuchMethodError: 类 'String' 没有实例方法 'tostring'。

我尝试在 pubspec.yaml 中添加依赖项: 依赖关系: to_string: ^1.2.1 dev_dependencies: to_string_generator: ^1.2.1

但我仍然在我的控制台上收到回复,而不是在应用程序上收到机器人的回复。

Please have a look.

好的耶耶耶!我确实将所有实例都更改为 .toString() ,而不是 .tostring() (这对我来说太愚蠢了......- __________-)

但现在我有一个错误: ════════ 小部件库捕获的异常══════════════════════════════════════════ ═════════════════════ 以下断言被抛出: type 'String' 不是 type 'Widget' 的子类型

【问题讨论】:

  • 该方法被称为toString。注意大写S

标签: flutter tostring nosuchmethoderror


【解决方案1】:

查看错误信息,aiResponse.getListMessage()[0]["text"]["text"][0] 已经返回一个 String 值,所以你可以删除 tostring() Lik Almasfiza 已经提到的,通常是toString() 带有大写的 S,但不需要将字符串转换为字符串。

【讨论】:

  • 谢谢,我把 .toString() 一起删除了。 :0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
相关资源
最近更新 更多