我有一个使用Flutter开发的简单游戏,可以登录用户,但是我不知道如何检查用户是否已登录以及如何获取用户数据。
对于登录,我正在使用此方法,登录用户后,我可以获取他的数据:
Future<String> signInWithGoogle() async{
print("INICIALIZANDO LOGIN COM GOOGLE...");
googleSignInAccount = await googleSignIn.signIn();
googleSignInAuthentication = await googleSignInAccount.authentication;
final AuthCredential credential = GoogleAuthProvider.getCredential(
accessToken: googleSignInAuthentication.accessToken,
idToken: googleSignInAuthentication.idToken,
);
AuthResult authResult = await _auth.signInWithCredential(credential);
user = authResult.user;
assert(!user.isAnonymous);
assert(await user.getIdToken() != null);
currentUser = await _auth.currentUser();
assert(user.uid == currentUser.uid);
uid = user.uid;
name = user.displayName;
email = user.email;
imageUrl = user.photoUrl;
print(uid);
print(email);
print(name);
print(imageUrl);
return 'signInWithGoogle succeded: $user';
}
并为此退出:
void signOutGoogle() async{
print("DESLOGOU!");
await googleSignIn.signOut();
}
答案 0 :(得分:1)
在检查用户是否登录时,我使用的是rootscreen,它只是一个空白页面,而用户几乎看不到它。 这是我的代码
class RootScreen extends StatefulWidget {
@override
State<StatefulWidget> createState() => new _RootScreenState();
}
class _RootScreenState extends State<RootScreen> {
@override
Widget build(BuildContext context) {
return new StreamBuilder<FirebaseUser>(
stream: FirebaseAuth.instance.onAuthStateChanged,
builder: (BuildContext context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return new Container(
color: Colors.white,
);
} else {
if (snapshot.hasData) {
//user is logged in
return new MainScreen(
firebaseUser: snapshot.data,
);
} else {
//user not logged in
return Login();
}
}
},
);
}
}
用于获取用户数据
class MainScreen extends StatelessWidget {
final GoogleSignInAccount googleUser;
final FirebaseUser firebaseUser;
const MainScreen(
{Key key, @required this.googleUser, @required this.firebaseUser})
: assert(googleUser != null),
assert(firebaseUser != null),
super(key: key);
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Offstage(
offstage: firebaseUser.photoUrl == null,
child: CircleAvatar(
backgroundImage: NetworkImage(firebaseUser.photoUrl)),
),
SizedBox(height: 8.0),
Text(firebaseUser.displayName, style: theme.textTheme.title),
Text(firebaseUser.email),
Text(firebaseUser.phoneNumber, style: theme.textTheme.subhead),
SizedBox(height: 16.0),
FlatButton(
child: Text("Sign out", style: theme.textTheme.button),
onPressed: () async {
await GoogleSignIn().signOut();
await FirebaseAuth.instance.signOut();
Navigator.of(context).pushAndRemoveUntil(
CupertinoPageRoute(builder: (context) => AuthScreen()),
(route) => false,
);
},
)
],
),
),
);
}
}
答案 1 :(得分:0)
MyAppClient.dart
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: GSTheme.backgroundColor,
),
debugShowCheckedModeBanner: false,
home: MyAppClient().getCurrentUser() != null
? HomeViewController()
: LoginViewController(),
);
Main.dart
{{1}}