flutter ListTile显示所有元素,而不仅仅是所需的元素

eqqqjvef  于 2023-01-27  发布在  Flutter
关注(0)|答案(1)|浏览(153)

问题您好,我在firebase中有一个Map字段,名为“fifthQuestion”,它包含两个选项(“黑色”或“棕色”)。当用户选择其中一个选项时,它们将作为布尔值保存在firebase中。我的代码(见下文)当前显示正确的元素(用户选择“黑色”),但是它显示了两次(一次用于“黑色”指数,一次用于“棕色”指数--尽管“棕色”没有被选中)。如何编辑代码,使其只显示用户选择(标记为true)的元素的索引,而不是多次显示正确的元素(无论索引有多少,即使它们没有标记为true)?

任何帮助将不胜感激,并提前感谢您。

消防基地屏幕截图

应用程序屏幕截图


当前代码

if(snapshot.connectionState == ConnectionState.done) {
                                Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
                                Map<String, dynamic> firstQuestion = data["fifthQuestion"];
                                List<String> keys = firstQuestion.keys.where((key) => firstQuestion[key] == true).toList();
                                var isTrue = keys;
                                return Column(
                                  children: [
                                    new ListTile(
                                      dense: true,
                                      tileColor: SkapeColors.pageBackground,
                                      title: Row(
                                        children: [
                                          Container(
                                            width: 250,
                                            child:
                                            new Text(
                                              isTrue[0],
                                              style: TextStyle(
                                                fontSize: 16,
                                                fontWeight: FontWeight.bold,
                                                letterSpacing: 0.5,
                                                color: Colors.white,
                                              ),
                                            ),
                                          ),
                                          Icon(Icons.check_circle, color: SkapeColors.colorPrimary,).paddingOnly(left: 20),
                                        ],
                                      ),
                                    ),
                                  ],
                                );
                              }
yrwegjxp

yrwegjxp1#

解决方案

Card(
              child: new FutureBuilder(
                future: jobs.doc(widget.jobId).get(),
                builder: (BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
                  if(snapshot.connectionState == ConnectionState.done) {
                    Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
                    Map<String, dynamic> firstQuestion = data["firstQuestion"];
                    List<String> keys = firstQuestion.keys.where((key) => firstQuestion[key] == true).toList();
                    var isTrue = keys;
                    return ListView.builder(
                        shrinkWrap: true,
                        // key: PageStorageKey<String>("Color Preference"),
                        physics: NeverScrollableScrollPhysics(),
                        itemCount: keys.length,
                        itemBuilder: (BuildContext context, int index) {
                          return Column(
                            children: [
                              new ListTile(
                                dense: true,
                                tileColor: SkapeColors.pageBackground,
                                title: Row(
                                  children: [
                                    Container(
                                      width: 250,
                                      child:
                                      new Text(
                                        isTrue[index],
                                        style: TextStyle(
                                          fontSize: 16,
                                          fontWeight: FontWeight.bold,
                                          letterSpacing: 0.5,
                                          color: Colors.white,
                                        ),
                                      ),
                                    ),
                                    Icon(Icons.check_circle,
                                      color: SkapeColors.colorPrimary,)
                                        .paddingOnly(left: 20),
                                  ],
                                ),
                              ),
                            ],
                          );
                        }
                    );
                  }
                  return Container();
                },

              ),
            )

相关问题