【问题标题】:Flutter: Output and Record information of checkboxes in StreamBuilderFlutter:StreamBuilder中复选框的输出和记录信息
【发布时间】:2022-06-16 15:41:35
【问题描述】:

如何为 Streambuilder 中返回的每个项目输出复选框,并记录该信息以便以后对其进行处理?我不知道将输出多少项目,所以我不知道应该使用多少变量来检查复选框的状态。我还附上了一个基本的 UI 来勾勒出我希望 streambuilder 看起来和记录的内容。 注意:我目前处于无状态小部件中,但是,如有必要,我可以将其更改为有状态

StreamBulder 的代码

StreamBuilder<QuerySnapshot>(
                                      stream: FirebaseFirestore.instance
                                          .collection("children")
                                          .where("parentUID",
                                              isEqualTo: FirebaseAuth
                                                  .instance.currentUser!.uid)
                                          .orderBy('timestamp',
                                              descending: true)
                                          .snapshots(),
                                      builder: (BuildContext context,
                                          AsyncSnapshot<QuerySnapshot>
                                              snapshot) {
                                        if (snapshot.hasError) {
                                          return const Text(
                                              'Something went wrong');
                                        }

                                        if (snapshot.connectionState ==
                                            ConnectionState.waiting) {
                                          return const Text("Loading");
                                        }
                                        return Column(
                                          children: snapshot.data!.docs.map(
                                            (DocumentSnapshot document) {
                                              Map<String, dynamic> data =
                                                  document.data()!
                                                      as Map<String, dynamic>;

                                              return Row(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: [
                                                  /*Output a checkbox for every result returned and name its title 'data['childUserName']'. Then, I want to be able to record the responses of those checkboxes and save them when I run a function.*/
                                                ],
                                              );
                                            },
                                          ).toList(),
                                        );
                                      },
                                    ),

UIX

Firebase 图片

【问题讨论】:

    标签: flutter


    【解决方案1】:

    制作bool列表并在for循环中使用它

    这是一个简单的例子:

    List<bool> checkboxValues = [];
    int i = 0;
    
    for (child in childern) {
      checkboxValues.add(false);
      i++;
    }
    
    CheckBox(
      value: checkboxValues[i],
      onChanged: (value) {
        checkboxValues[i] = value;
      },
    )
    

    【讨论】:

      猜你喜欢
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-07
      • 1970-01-01
      相关资源
      最近更新 更多