如何使用导航到 setState() 另一个屏幕?

问题描述

我在颤动中遇到抽屉问题我需要在我登录时隐藏 listtittle 中的登录但它无法通过可见方式工作我认为原因是登录方法在另一个屏幕上 这是抽屉的代码

if (_auth.currentUser == null)
                Column(
                  children: [
                    Visibility(
                      visible: _visible,child: ListTile(
                        title: Text('Sign In'),onTap: () {
                          setState(() {});

                          Navigator.push(
                            context,MaterialPageRoute(
                                builder: (context) => SignIn()),);
                        },),],)
              else
                Column(
                  children: [
                    Visibility(
                      visible: _unvisible,child: ListTile(
                        title: Text('My Cars'),onTap: () {
                          Navigator.push(
                            context,MaterialPageRoute(
                                builder: (context) => MyCars()),Visibility(
                      visible: _unvisible,child: ListTile(
                        title: Text('Sign Out'),onTap: () {
                          signOut();
                          setState(() {});
                        },

注意:当我按退出时它正在工作,退出列表标题消失并且登录可见但是当我登录时我需要刷新页面

这是登录按钮

new RaisedButton(
          onpressed: () {
            signin();

            Navigator.push(
              context,MaterialPageRoute(builder: (context) => HomeScreen()),);
          },child: new Text('Sign In'),

注意:这两段代码来自不同的类

解决方法

除非我误解了这种情况,否则 _auth.currentUser 似乎没有正确保存。对于简单的修复,您应该在用户登录时实现类似 shared_preferences 的内容:

 //import
 import 'package:shared_preferences/shared_preferences.dart';


 //somewhere in your login code put:
 SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
 sharedPreferences.setString("logged_in","true");

我个人喜欢在 initState 中调用 shared_preferences:

 SharedPreferences sharedPreferences;

  @override
  void initState() {
    getSharedPrefs();
    super.initState();
  }



 Future<Null> getSharedPrefs() async {
    sharedPreferences = await SharedPreferences.getInstance();
  }

然后更改以下内容:

if (_auth.currentUser == null)

if (sharedPreferences.get("logged_in") == null)

注销:

sharedPreferences.remove('logged_in');

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...