'showSnackBar'已过时,不应使用 [英] 'showSnackBar' is deprecated and shouldn't be used

查看:226
本文介绍了'showSnackBar'已过时,不应使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图弄清楚这个颤动的问题.下面的代码不推荐使用showSnackbar,并且正在尝试解决此问题.第二个代码是我尝试解决的问题.未为类型'ScaffoldState'定义吸气剂'ScaffoldMessenger'"提出了一个新问题.该错误告诉我要导入material.dart文件,但该文件已被导入.

Trying to figure out this flutter problem. The below code has the showSnackbar as deprecated, and am trying to figure out with the fix is. The second code is my attempt to fix the problem. A new problem comes up with "The getter 'ScaffoldMessenger' isn't defined for the type 'ScaffoldState'.". The error tells me to import material.dart file but it is already imported.

感谢您的帮助.

              Padding(
                padding: const EdgeInsets.all(10.0),
                child: GestureDetector(
                  onTap: ()async{
                    if(!await authProvider.signIn()){
                      _key.currentState.showSnackBar(
                        SnackBar(content: Text("Login failed"))
                      );
                    }
                  },

              Padding(
                padding: const EdgeInsets.all(10.0),
                child: GestureDetector(
                  onTap: ()async{
                    if(!await authProvider.signIn()){
                      _key.currentState.ScaffoldMessenger.of(context).showSnackBar(
                        SnackBar(content: Text("Login failed"))
                      );
                    }
                  },

推荐答案

基于此处的文档,看起来新的ScaffoldMessenger可以处理其下面的所有SnackBar.如果您没有多个ScaffoldMessengers,则应该可以致电:

Based on the documentation here, it looks like the new ScaffoldMessenger handles all SnackBars below it. If you don't have multiple ScaffoldMessengers, you should just be able to call:

ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Login failed")));

这篇关于'showSnackBar'已过时,不应使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆