首页 > 解决方案 > 没有为“BuildContext”类型定义方法“showSnackBar”

问题描述

在这里,我创建了一个 DropdownItemMenu,它从 firebase 集合中获取元素,但它显示了一个错误

没有为“BuildContext”类型定义方法“showSnackBar”

Container(
                    margin: EdgeInsets.only(left: 16, right: 16),
                    height: MediaQuery.of(context).size.height * 0.10,
                    child: Card(
                      child: Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: DropdownButton<String>(
                          isExpanded: false,
                          style: TextStyle(
                              fontSize: 14, color: Colors.blueGrey),
                          icon: const Icon(Icons.keyboard_arrow_down),
                          underline: Container(color: Colors.transparent),
                          onChanged: (serieValue) {
                            final snackBar = SnackBar(
                                content: Text(
                              'Série selectionnée est $serieValue',
                              style: TextStyle(
                                  fontWeight: FontWeight.bold,
                                  fontSize: 14,
                                  color: Color(0xff11b719)),
                            ));
                            Scaffold.of(context.showSnackBar(snackBar));


                            setState(() {
                              selectedIndexSerie = serieValue;
                            });
                          },
                          value: selectedIndexSerie,
                          hint: Text(
                            'Selectionner une série',
                            style: TextStyle(
                                fontWeight: FontWeight.bold,
                                fontSize: 14,
                                color: Color(0xff11b719)),
                          ),
                          items: series,
                        ),
                      ),
                    ),
                  );

标签: firebasefluttersnackbarflutter-dropdownbutton

解决方案


你放错了')' -->

Scaffold.of(context).showSnackBar(snackBar);

此外,不推荐使用 showSnackBar,请考虑使用 ScaffoldMessenger

ScaffoldMessenger.of(context).showSnackBar(snackBar);

推荐阅读