firebase - 谷歌卡在登录屏幕上叹了口气
问题描述
我在使用谷歌时遇到问题,当我登录时,我收到了我在网站中使用我的帐户的邮件,但它没有更改 isSignedIn =>true 的值,我使用 firebase 作为后端我确实添加了支持电子邮件并链接我的数据库到我的项目,但似乎有一些逻辑错误 这是代码
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
final GoogleSignIn gSignIn = GoogleSignIn();
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage>
{
bool isSignedIn = false;
void iniState(){
super.initState();
gSignIn.onCurrentUserChanged.listen((gSigninAccount) {
controlSignIn(gSigninAccount);
}, onError: (gError){
print("ERROR MESSAGE"+ gError);
});
gSignIn.signInSilently(suppressErrors: false).then((gSigninAccount){
controlSignIn(gSigninAccount);
}).catchError((gError){
print("ERROR MESSAGE"+ gError);
});
}
controlSignIn(GoogleSignInAccount signInAccount) async {
if(signInAccount != null){
setState(() {
isSignedIn = true;
});
}
else{
setState(() {
isSignedIn = false;
});
}
}
loginUser(){
gSignIn.signIn();
}
logoutUser(){
gSignIn.signOut();
}
Scaffold buildHomeScreen(){
return RaisedButton.icon(onPressed: logoutUser, icon: Icon(Icons.close), label: Text("Sign Out"));
}
Scaffold buildSignInScreen(){
return Scaffold(
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topRight,
end: Alignment.bottomLeft,
colors: [Theme.of(context).accentColor, Theme.of(context).primaryColor],
)
),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
"SS",
style:TextStyle(fontSize: 65.0, color: Colors.white, fontFamily: "Signatra" ),
),
GestureDetector(
onTap: loginUser,
child: Container(
width:270.0,
height: 65.0,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage("assets/images/google_signin_button.png"),
fit: BoxFit.cover,
)
),
),
)
],)
), );}
这是我用来检查值是否已更改的方法
@override
Widget build(BuildContext context) {
if (isSignedIn)
{
return buildHomeScreen();
}
else
{
return buildSignInScreen();
}
}
}
yaml
name: buddiesgram
description: A new Flutter project.
# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cloud_firestore: ^0.9.7+2
image: ^2.0.7
animator: 0.1.4
image_picker: ^0.6.0+2
google_sign_in: ^4.0.1+1
timeago: 2.0.17
cached_network_image:
firebase_auth: ^0.8.3
geolocator: 5.0.1
uuid: ^2.0.0
cupertino_icons: ^0.1.2
path_provider: ^0.5.0+1
firebase_messaging: ^4.0.0+1
firebase_storage: ^2.1.0+1
flutter_svg:
dev_dependencies:
flutter_test:
sdk: flutter
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec
# The following section is specific to Flutter.
flutter:
uses-material-design: true
fonts:
- family: Signatra
fonts:
- asset: assets/fonts/Signatra.ttf
assets:
- assets/images/google_signin_button.png
解决方案
要将 Firebase 与您的 Flutter 项目一起使用,最好的方法是使用 FlutterFire。您需要按照这些步骤进行配置。
就像您对我说的那样,您将遇到与其他 firebase 软件包的兼容性问题。所以你需要像这样修复它们:
首先你需要添加包firebase_core: ^0.7.0
然后在您的 pubspec.yaml 中更改这些包
cloud_firestore: ^0.9.7+2
firebase_auth: ^0.8.3
firebase_messaging: ^4.0.0+1
firebase_storage: ^2.1.0+1
至
cloud_firestore: ^0.16.0
firebase_auth: ^0.20.1
firebase_messaging: ^8.0.0-dev.14
firebase_storage: ^7.0.0
这将解决所有兼容问题。而且,如果您运行该命令flutter pub get
,则不应出现错误。
您现在可以执行此操作来检查您的用户是否已连接
/*
* If FirebaseAuth.instance.currentUser return null
* that mean you user is not logged. Otherwise you get
* a value of type User with the user data
*/
@override
Widget build(BuildContext context) {
return FirebaseAuth.instance.currentUser == null
? buildSignInScreen()
: buildHomeScreen();
}
您可以在此处找到有关用户管理的文档
推荐阅读
- neo4j - Neo4j 中特定 DBMS 的连接问题 localhost7474
- scala - Scala:如何将任何通用序列作为此方法的输入
- android - 带有约束布局的 TextView 走出屏幕
- c# - 来自 SQLCLR 的异步 API 调用中的问题
- python - 如何在 Python 中使用 click.option() 接收一串句子?
- android - 除了使用 FFMPEG 和 MP4Parser 之外,还有什么方法可以修剪视频吗?
- php - 如何使用引导程序 4 显示来自数据库的 3 列动态数据
- hibernate - 尝试在 Hibernate 5.x 中加载记录时,默认构造函数被多次执行
- python - 如何在 Visual Studio 中升级 Django
- c# - 需要帮助使用 DI、C# 和 Web API 2.0 使用条件参数初始化类的建议