【问题标题】:Flutter: How to store or hold the value of the button that user has been clicked?Flutter:如何存储或保存用户点击的按钮的值?
【发布时间】:2021-11-15 17:22:06
【问题描述】:

我做了一个有 4 个按钮选项的测验问题。

我在一页上显示几个问题。 Example of the quiz page

现在如何在我为 POST 请求 API 传递值之前存储或保存用户单击的每个按钮的值。

目前我知道要做的就是保存用户点击的最新值。

顺便说一句,我是编码和颤振的新手。希望大家能帮帮我。

这是我做的代码

                List<dynamic> sent;

                                        CustomCheckBoxGroup(
                                          buttonValuesList: [
                                            [
                                              snapshot.data.result[index].id,
                                              snapshot.data.result[index]
                                                  .options[0].id
                                            ],
                                            [
                                              snapshot.data.result[index].id,
                                              snapshot.data.result[index]
                                                  .options[1].id
                                            ],
                                            [
                                              snapshot.data.result[index].id,
                                              snapshot.data.result[index]
                                                  .options[2].id
                                            ],
                                            [
                                              snapshot.data.result[index].id,
                                              snapshot.data.result[index]
                                                  .options[3].id
                                            ],
                                          ],
                                          buttonLables: [
                                            snapshot.data.result[index]
                                                .options[0].option,
                                            snapshot.data.result[index]
                                                .options[1].option,
                                            snapshot.data.result[index]
                                                .options[2].option,
                                            snapshot.data.result[index]
                                                .options[3].option,
                                          ],
                                          checkBoxButtonValues: (values) {
                                            sent=values;
                                            print(values);
                                          },
                                          selectedColor: Colors.orange,
                                          unSelectedColor: Colors.white70,
                                          horizontal: true,
                                        ),

我想为 POST API 发送数组中的值

喜欢这个

{
    "data":[{"question_id":1,"option_id":2},{"question_id":2,"option_id":6}]
}

虽然这是我为 post API 做的事情

  Future<Sent> saveAnswer() async {
    setState(() {
      _isloading = true;
    });

    // var data = {"question_id": sent[0][0], "option_id": sent[0][1]};
    Map<String, List<Map<String, int>>> data = {
      "data": [
        {"question_id": sent[0][0], "option_id": sent[0][1]},
        {"question_id": sent[0][0], "option_id": sent[0][1]},
      ]
    };

    var url = await Network().link("/exercise/1/saveAnswer");
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    final token = jsonDecode(localStorage.getString('token'));
    http.Response response =
        await http.post(Uri.parse(url), body: json.encode(data), headers: {
      'Content-type': 'application/json',
      'Accept': 'application/json',
      'Authorization': 'Bearer $token'
    });
    if (response.statusCode == 200) {
      print(response.body);
      print("Save success");
    } else {
      print(response.body);
      print("Save failed");
    }
  }

这是我点击按钮时颤动的响应

我/颤振(4884):[[1, 2]]

我/颤振(4884):[[2, 7]]

[question_id, option_id]

【问题讨论】:

    标签: arrays flutter http-post custom-button


    【解决方案1】:

    我认为使用RadioListTile 进行多项选择比手动为每个问题使用 4 个按钮要好。 您可以创造性地制作List 变量来动态存储问题和答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-30
      • 2018-02-18
      相关资源
      最近更新 更多