我的Google登录按钮首先进入首页,然后显示登录选项(电子邮件帐户)。在这里做什么?

时间:2019-05-16 09:59:41

标签: firebase dart flutter firebase-authentication google-authentication

我正在开发我的项目。我正在尝试使用Firebase的Google登录身份验证来使登录页面更加混乱。我已经构建了它,但是当我单击Google登录按钮时,它首先进入首页,然后显示登录选项,但是反之亦然。在这里帮助我。

这是登录页面Login Page

当我点击“使用Google登录”按钮enter image description here时显示。

“现在终于登录”选项出现enter image description here

这是我的密码

Google登录身份验证

//google sign in
 final GoogleSignIn googleSignIn=  GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;
  Future<FirebaseUser> _signIn() async{
    //GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
    GoogleSignInAccount googleUser = await googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    final AuthCredential credential = GoogleAuthProvider.getCredential(
      idToken: googleAuth.idToken,
      accessToken: googleAuth.accessToken,
    );
    final FirebaseUser user = await _auth.signInWithCredential(credential);
    print("User Name: ${user.displayName}");
    assert(await user.getIdToken() != null);
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    return user;
  }

登录按钮

RaisedButton(
                child: Text('Login with Google'),
                color: Colors.orange,
                textColor: Colors.white,
                elevation: 7.0,
                onPressed: () { _signIn();
                  Navigator.of(context).pushReplacementNamed('/homepage');
                }
              ),

3 个答案:

答案 0 :(得分:0)

如果您不执行任何操作,为什么要在function convertSecToTime($sec) { $date1 = new DateTime("@0"); $date2 = new DateTime("@$sec"); $interval = date_diff($date1, $date2); $parts = ['years' => 'y', 'months' => 'm', 'days' => 'd', 'hours' => 'h', 'minutes' => 'i', 'seconds' => 's']; $formatted = []; foreach($parts as $i => $part) { $value = $interval->$part; if ($value !== 0) { if ($value == 1){ $i = substr($i, 0, -1); } $formatted[] = "$value $i"; } } if (count($formatted) == 1) { return $formatted[0]; } else { $str = implode(', ', array_slice($formatted, 0, -1)); $str.= ' and ' . $formatted[count($formatted) - 1]; return $str; } } 中返回用户?

相反,我将导航语句移到_signIn()的末尾,因为_signIn()_signIn()

async
//google sign in
  final GoogleSignIn googleSignIn=  GoogleSignIn();
  final FirebaseAuth _auth = FirebaseAuth.instance;
  Future<FirebaseUser> _signIn() async{
    //GoogleSignInAccount googleSignInAccount = await googleSignIn.signIn();
    GoogleSignInAccount googleUser = await googleSignIn.signIn();
    GoogleSignInAuthentication googleAuth = await googleUser.authentication;
    final AuthCredential credential = GoogleAuthProvider.getCredential(
      idToken: googleAuth.idToken,
      accessToken: googleAuth.accessToken,
    );
    final FirebaseUser user = await _auth.signInWithCredential(credential);
    print("User Name: ${user.displayName}");
    assert(await user.getIdToken() != null);
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(user.uid == currentUser.uid);
    Navigator.of(context).pushReplacementNamed('/homepage');
  }

答案 1 :(得分:0)

另一种可能性是:

RaisedButton(
            child: Text('Login with Google'),
            color: Colors.orange,
            textColor: Colors.white,
            elevation: 7.0,
            onPressed: () {
              _signIn().then(() 
               {Navigator.of(context).pushReplacementNamed('/homepage')}); 
            }
          ),

答案 2 :(得分:0)

您正在按按钮导航到主屏幕-

RaisedButton(
                    child: Text('Login with Google'),
                    color: Colors.orange,
                    textColor: Colors.white,
                    elevation: 7.0,
                    onPressed: () { _signIn();
                      Navigator.of(context).pushReplacementNamed('/homepage');
                    }
                  ),

应如下所示-

RaisedButton(
                    child: Text('Login with Google'),
                    color: Colors.orange,
                    textColor: Colors.white,
                    elevation: 7.0,
                    onPressed: () { _signIn();

                    }
                  ),

从Google登录名获得正确响应后,您可以通过添加“ Navigator.of(context).pushReplacementNamed('/ homepage');”来导航到主屏幕。