Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

flutter - 'showSnackBar' is deprecated and shouldn't be used

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.

Any help is appreciated.

              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"))
                      );
                    }
                  },
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...