Firebase 颤振页面视图未在将来的生成器异步快照中更新

Firebase 颤振页面视图未在将来的生成器异步快照中更新,firebase,authentication,flutter,future,Firebase,Authentication,Flutter,Future,我使用的代码来自-> 当我使用Firebase登录时,我希望页面自动重定向到主页 main.dart的代码如下所示: void main() { // setupLocator(); SystemChrome.setPreferredOrientations( [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]) .then((_) => runApp(

我使用的代码来自->

当我使用Firebase登录时,我希望页面自动重定向到主页

main.dart的代码如下所示:

void main() {
  // setupLocator();
  SystemChrome.setPreferredOrientations(
          [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown])
      .then((_) => runApp(
            ChangeNotifierProvider<AuthService>(
              child: MyApp(),
              builder: (BuildContext context) {
                return AuthService();
              },
            ),
          ));
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      onGenerateRoute: Router.generateRoute,
      theme: ThemeData(primarySwatch: Colors.blue),
      home: FutureBuilder<FirebaseUser>(
        future: Provider.of<AuthService>(context).getUser(),
        builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
          if (snapshot.connectionState == ConnectionState.done) {
            // log error to console
            if (snapshot.error != null) {
              print("error");
              return Text(snapshot.error.toString());
            }
            // redirect to the home page
            return snapshot.hasData
                ? CustomerHomePage(snapshot.data)
                : LoginPage();
          } else {
            // show loading indicator
            return LoadingCircle();
          }
        },
      ),
    );
  }
}
void main(){
//setupLocator();
SystemChrome.setPreferredOrientations(
[设备定向。肖像向上,设备定向。肖像向下])
.然后(())=>runApp(
变更通知提供者(
子项:MyApp(),
生成器:(BuildContext上下文){
返回AuthService();
},
),
));
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
onGenerateRoute:Router.generateRoute,
主题:主题数据(原始样本:颜色。蓝色),
家:未来建设者(
future:Provider.of(context.getUser(),
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.done){
//将错误记录到控制台
如果(snapshot.error!=null){
打印(“错误”);
返回文本(snapshot.error.toString());
}
//重定向到主页
返回snapshot.hasData
?客户主页(快照数据)
:LoginPage();
}否则{
//显示加载指示器
返回加载循环();
}
},
),
);
}
}
登录页面代码为:

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final _formKey = GlobalKey<FormState>();
  String _password;
  String _email;

  FocusNode nodeOne = FocusNode();
  FocusNode nodeTwo = FocusNode();

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



  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: prefix0.backgroundColor,
      body: Form(
        key: _formKey,
        child: Padding(
          padding: EdgeInsets.all(20.0),
          child: Stack(
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                  TextFormField(
                    style: TextStyle(color: Colors.white),
                    textInputAction: TextInputAction.next,
                    focusNode: nodeOne,
                    onSaved: (value) => _email = value,
                    keyboardType: TextInputType.emailAddress,
                    decoration: InputDecoration(
                      prefixIcon:
                          Image.asset('assets/images/icons/Username.png'),
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.grey),
                      ),
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(color: Colors.white),
                      ),
                      labelText: "Email Address",
                      labelStyle: TextStyle(
                          color: nodeOne.hasFocus ? Colors.white : Colors.grey),
                    ),
                  ),
                  TextFormField(
                      style: TextStyle(color: Colors.white),
                      textInputAction: TextInputAction.go,
                      focusNode: nodeTwo,
                      onSaved: (value) => _password = value,
                      obscureText: true,
                      decoration: InputDecoration(
                          prefixIcon: Image.asset(
                              'assets/images/icons/PasswordLock.png'),
                          enabledBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                          ),
                          focusedBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.white),
                          ),
                          labelStyle: TextStyle(
                              color: nodeTwo.hasFocus
                                  ? Colors.white
                                  : Colors.grey),
                          labelText: "Password")),
                  SizedBox(height: 20.0),
                  Center(
                    child: FlatButton(
                      onPressed: () {},
                      child: Text('Forgotten password?',
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Colors.white)),
                    ),
                  ),
                  ButtonTheme(
                    minWidth: MediaQuery.of(context).size.width,
                    height: 40,
                    child: RaisedButton(
                        textColor: prefix0.backgroundColor,
                        color: Colors.white,
                        child: Text("Login"),
                        onPressed: () {
                          _loginAction();
                        },
                        shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(5.0))),
                  ),

                  Center(
                    child: FlatButton(
                      onPressed: () {},
                      child: Text('Not registered?',
                          textAlign: TextAlign.center,
                          style: TextStyle(color: Colors.white)),
                    ),
                  ),
                  Divider(
                    indent: MediaQuery.of(context).size.width * 0.333333,
                    height: 5,
                    thickness: 3,
                    color: Colors.white,
                    endIndent: MediaQuery.of(context).size.width * 0.333333,
                  ),
                  SizedBox(height: 15),
                  ButtonTheme(
                    minWidth: MediaQuery.of(context).size.width,
                    height: 40,
                    child: RaisedButton(
                        textColor: prefix0.backgroundColor,
                        color: Colors.white,
                        child: Text("Not a customer? Visit out site"),
                        onPressed: () {},
                        shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(5.0))),
                  ),
                  SizedBox(height: 20),
                ],
              ),
            ],
          ),
        ),
      ),
    );
  }

  _loginAction() async {
    final form = _formKey.currentState;
    form.save();

    // Validate will return true if is valid, or false if invalid.
    if (form.validate()) {
      try {
        FirebaseUser result = await Provider.of<AuthService>(context)
            .loginUser(email: _email, password: _password);
        print(result);

      } on AuthException catch (error) {
        return _buildErrorDialog(context, error.message);
      } on Exception catch (error) {
        return _buildErrorDialog(context, error.toString());
      }
    }
  }

  Future _buildErrorDialog(BuildContext context, _message) {
    return showDialog(
      builder: (context) {
        return AlertDialog(
          title: Text('Error Message'),
          content: Text(_message),
          actions: <Widget>[
            FlatButton(
                child: Text('Cancel'),
                onPressed: () {
                  Navigator.of(context).pop();
                })
          ],
        );
      },
      context: context,
    );
  }
}
class LoginPage扩展StatefulWidget{
@凌驾
_LoginPagentate createState()=>_LoginPagentate();
}
类_loginpagentate扩展状态{
final _formKey=GlobalKey();
字符串\u密码;
字符串\u电子邮件;
FocusNode nodeOne=FocusNode();
FocusNode nodewo=FocusNode();
@凌驾
void initState(){
super.initState();
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
backgroundColor:prefix0.backgroundColor,
正文:表格(
键:_formKey,
孩子:填充(
填充:所有边缘设置(20.0),
子:堆栈(
儿童:[
纵队(
crossAxisAlignment:crossAxisAlignment.start,
mainAxisAlignment:mainAxisAlignment.end,
儿童:[
TextFormField(
样式:TextStyle(颜色:Colors.white),
textInputAction:textInputAction.next,
focusNode:nodeOne,
onSaved:(值)=>\u email=value,
键盘类型:TextInputType.emailAddress,
装饰:输入装饰(
前缀:
Image.asset('assets/images/icons/Username.png'),
enabledBorder:UnderlineInputBorder(
borderSide:borderSide(颜色:Colors.grey),
),
FocusedOrder:下划线输入边框(
borderSide:borderSide(颜色:Colors.white),
),
labelText:“电子邮件地址”,
标签样式:文本样式(
颜色:nodeOne.hasFocus?颜色。白色:颜色。灰色),
),
),
TextFormField(
样式:TextStyle(颜色:Colors.white),
textInputAction:textInputAction.go,
focusNode:nodewo,
onSaved:(值)=>\u密码=值,
蒙昧文字:对,
装饰:输入装饰(
前缀:Image.asset(
“资产/图像/图标/密码锁.png”),
enabledBorder:UnderlineInputBorder(
borderSide:borderSide(颜色:Colors.grey),
),
FocusedOrder:下划线输入边框(
borderSide:borderSide(颜色:Colors.white),
),
标签样式:文本样式(
颜色:nodeTwo.hasFocus
?颜色:白色
:颜色。灰色),
labelText:“密码”),
尺寸箱(高度:20.0),
居中(
孩子:扁平按钮(
按下:(){},
子项:文本('忘记密码?',
textAlign:textAlign.center,
样式:TextStyle(颜色:Colors.white)),
),
),
钮扣(
minWidth:MediaQuery.of(context).size.width,
身高:40,
孩子:升起按钮(
textColor:prefix0.backgroundColor,
颜色:颜色,白色,
子项:文本(“登录”),
已按下:(){
_loginAction();
},
形状:新的RoundedRectangleBorder(
borderRadius:新的borderRadius.circular(5.0)),
),
居中(
孩子:扁平按钮(
按下:(){},
子项:文本('未注册?',
textAlign:textAlign.center,
样式:TextStyle(颜色:Colors.white)),
),
),
分隔器(
缩进:MediaQuery.of(context).size.width*0.333,
身高:5,,
厚度:3,
颜色:颜色,白色,
endIndent:MediaQuery.of(context).size.width*0.333333,
),