首页 > 解决方案 > Flutter中的错误:flutter:错误:NoSuchMethodError:方法'findAncestorStateOfType'在null上被调用

问题描述

一旦用户通过 firebase 身份验证,我将返回 UID,但随后无法打开主页。我可以在控制台上打印用户 IF 但它没有转移到下一页

验证和提交()异步{

  if (validateandSave()) {
  try {


            FirebaseUser user = (await FirebaseAuth.instance
                    .signInWithEmailAndPassword(email: _email, password: _password))
                .user; 

            print('User is valid and saved');
            print('Loggedin: USerId ${user.uid}');
            Navigator.of(context).pushReplacementNamed('/home');

          } catch (e) {
            print('Error : $e');
          }
        }
      }

      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
              title: Text('Fint'),
            ),
            body: Container(
                padding: EdgeInsets.all(16.0),
                child: new Form(
                    key: formKey,
                    child: new Column(children: <Widget>[

                      new TextFormField(
                        decoration: new InputDecoration(labelText: 'Email'),
                        validator: (value) =>
                            value.isEmpty ? 'Email cannot be empty' : null,
                        onSaved: (value) => _email = value,
                      ),
                      new TextFormField(
                        decoration: new InputDecoration(labelText: 'Password'),
                        validator: (value) =>
                            value.isEmpty ? 'Password cannot be empty' : null,
                        onSaved: (value) => _password = value,
                        obscureText: true,
                      ),
                      new RaisedButton(
                        child: Text('Login'),
                        onPressed: validateandSubmit
                      ),

                    ]))));
      }
    }

标签: iosflutterdart

解决方案


尝试这个

Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => Home()));

推荐阅读