【问题标题】:flutter type 'String' is not a subtype of type 'int' of 'index'颤振类型'String'不是'index'的'int'类型的子类型
【发布时间】:2023-03-31 03:35:02
【问题描述】:

尝试在颤振中运行时出现错误 (类型'String'不是'index'类型'int'的子类型)

void _submitForm() {
//cek validasi form
if (_formKey.currentState.validate()) {
  FormFeedback formFeedback = FormFeedback(
      nameController.text, emailController.text, feedbackController.text);

  FormController formController = FormController((String response) {
    if (response == FormController.STATUS_SUCCESS) {
      _showSnackbar("feedback berhasil disimpan");
    } else {
      _showSnackbar("Error");
    }
  });
  _showSnackbar("Menyimpan Feedback");
  formController.submitForm(formFeedback);
}

I/flutter (18110):“String”类型不是“index”的“int”类型的子类型

【问题讨论】:

  • 请将您的代码作为文本发布在代码时钟中。代码图片无法搜索并影响任何需要电子帮助的残障人士,因为常用工具在图片上的工作方式与在文本上的工作方式不同。最后但同样重要的是,我们不能只是将您的代码复制到我们的 IDE 中并在它是图片时对其进行测试。
  • 检查你的 json 输出,也许有些东西是空的。
  • 完成,我已编辑帖子
  • 我在下面发布了我的 controller.dart 代码

标签: flutter


【解决方案1】:

这是文件 controller.dart

class FormController {
  void Function(String) callback;

  static const String URL =
      "https://script.google.com/macros/s/AKfycbytRadl0y- 
       yAVxgtiKOYzXQFZND7tX085am06cm-a48l2CGb_6T/exec";
  static const STATUS_SUCCESS = "SUCCESS";

  FormController(this.callback);

  void submitForm(FormFeedback formFeedback) async {
   try {
     await http.get(URL + formFeedback.toParams()).then((value) =>
        callback(convert.jsonDecode(value.body)['status']));
   }   catch (e) {
       print(e);
   }
   }
  }

【讨论】:

    【解决方案2】:

    可能 'status' 是 int 类型并且回调需要 String。尝试将回调参数更改为 int:

    void Function(int) callback;
    

    或将其转换为字符串:

    callback(convert.jsonDecode(value.body)['status'].toString()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 2020-02-29
      • 2019-04-24
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      相关资源
      最近更新 更多