颤振类型“String”不是“index”的“int”类型的子类型

时间:2021-02-15 15:48:04

标签: flutter

尝试在颤振中运行时出错 ('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' 的子类型

2 个答案:

答案 0 :(得分:0)

这是文件 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);
   }
   }
  }

答案 1 :(得分:0)

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

void Function(int) callback;

或将其转换为字符串:

callback(convert.jsonDecode(value.body)['status'].toString()));
相关问题