无法通过调用 ScaffoldState().removeCurrentSnackBar() 删除当前的 snackbar

Cannot remove the current snackbar by calling ScaffoldState().removeCurrentSnackBar()

出于某种原因,即使我正在调用 ScaffoldState().removeCurrentSnackBar(),我也无法删除当前的 snackbar

我没有收到任何错误消息,但出于某种原因,小吃店不会消失。当用户从 DropdownButton 中选择项目时,还有其他方法可以删除快餐栏吗?

我已经尝试过 ScaffoldState().hideCurrentSnackBar() 和所有其他应该删除 SnackBar 的功能。

感谢您的回答。

class MainRoute extends StatefulWidget {
  @override
  _MainRouteState createState() => _MainRouteState();
}

class _MainRouteState extends State<MainRoute> {
  List<Currency> dropdownItems = [
    Currency(currencyName: "FOO", currencyInUSD: 22.0),
    Currency(currencyName: "BOO", currencyInUSD: 22.0),
    Currency(currencyName: "SOO", currencyInUSD: 22.0),
  ];
  Currency dropdownValue;

  Color color = Colors.green;
  MainModel model = MainModel();

  @override
  void initState() {
    super.initState();
    dropdownValue = dropdownItems[0];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      ///A Builder has been added so the context contains the Scaffold which
      ///is required to create a SnackBar
      floatingActionButton: Builder(
        builder: (context) {
          return FloatingActionButton(
            child: Icon(
              Icons.add,
              color: kIconColor,
            ),
            onPressed: () {
              print(dropdownItems.map((item) {
                return DropdownMenuItem(
                  value: item,
                  child: Text(item.currencyName),
                );
              }).toList());
              setState(() {
                Scaffold.of(context).showSnackBar(
                  SnackBar(
                    content: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: <Widget>[
                        Text('Which currency do you want to add to your list?'),
                        SizedBox(
                          height: 25.0,
                        ),
                        DropdownButton<Currency>(
                          items: dropdownItems
                              .map<DropdownMenuItem<Currency>>((item) {
                            return DropdownMenuItem(
                              value: item,
                              child: Text(item.currencyName),
                            );
                          }).toList(),
                          value: dropdownValue,
                          onChanged: (value) {
                            setState(() {
                              dropdownValue = value;
                              ScaffoldState().removeCurrentSnackBar(
                                  reason: SnackBarClosedReason.remove);
                              print("SnackBar removed");
                            });
                          },
                        )
                      ],
                    ),
                    action: SnackBarAction(
                      label: 'Cancel',
                      onPressed: () {
                        print('Cancel');
                      },
                    ),
                  ),
                );
              });
            },
          );
        },
      ),
    );
  }
} 

当用户从 DropdownButton 中选择一个项目时,而不是使用以下 onChanged 方法:

onChanged: (value) {
      setState(() {
         dropdownValue = value;
         ScaffoldState().removeCurrentSnackBar(
         reason: SnackBarClosedReason.remove);
         print("SnackBar removed");
      });
   ),

改用此方法调用来移除小吃店:

onChanged: (value) {
      setState(() {
         dropdownValue = value;
         Scaffold.of(context).removeCurrentSnackBar(
            reason: SnackBarClosedReason.remove,
         );
         print("SnackBar removed");
      });
   ),

至于 2021 年,您应该使用

ScaffoldMessenger.of(context).removeCurrentSnackBar()

而不是

Scaffold.of(context).removeCurrentSnackBar();

此功能在 Flutter v1.23.0-14 之后被弃用。0.pre。