[英]Using FirebaseAuth to Skip Login Screen for Logged In Users Flutter
我正在尝试使用 FirebaseAuth 跳过登录页面并直接进入我的应用程序中的主页,但firebaseAuth.getCurrentUser()
给出了两次结果,第一次为null
,第二次为FirebaseUser
。 而应用程序始终走的是第一个结果为THE
结果,这不是给我的登录屏幕。
我的代码如下;
@override
Widget build(BuildContext context) {
return FutureBuilder<FirebaseUser>(
future: authService.getCurrentUser(),
builder: (context, AsyncSnapshot<FirebaseUser> snapshot) {
print("snapshot data");
print(snapshot.hasData);
if (snapshot.hasData) {
print(snapshot.data.uid);
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
initialRoute: HomePage.id,
routes: {
HomePage.id: (context) => HomePage(snapshot.data.uid),
WelcomePage.id: (context) => WelcomePage(),
RegistrationPage.id: (context) => RegistrationPage()
},
);
} else {
return SafeArea(
child: Scaffold(
backgroundColor: colorPalette.white,
body: Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: SizedBox(
height: 150,
child: Image.asset(
"lib/assets/images/logo-with-name.png",
),
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 20.0, vertical: 10.0),
child: CustomTextBox(
size: 8.0,
hintText: "E-Mail",
borderColor: colorPalette.logoLightBlue,
function: onChangeFunction,
keyboardType: TextInputType.emailAddress,
),
),
Padding(
padding: EdgeInsets.symmetric(
horizontal: 20.0, vertical: 10.0),
child: CustomTextBox(
size: 8.0,
hintText: "Password",
borderColor: colorPalette.logoLightBlue,
function: onChangeFunctionTwo,
keyboardType: TextInputType.text,
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: EdgeInsets.only(
left: 10.0, top: 10.0, bottom: 10.0),
child: Material(
elevation: 5.0,
color: colorPalette.lighterPink,
borderRadius: BorderRadius.circular(10.0),
child: MaterialButton(
onPressed: () {
signIn();
},
child: Text(
"Log In",
style: TextStyle(
color: colorPalette.white,
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(10.0),
child: Text("or"),
),
GestureDetector(
onTap: () {
setState(() {
Navigator.pushNamed(
context, RegistrationPage.id);
});
},
child: Text(
"Register",
style: TextStyle(
fontWeight: FontWeight.bold,
color: colorPalette.logoLightBlue,
),
),
),
],
)
],
),
),
),
);
}
});
}
AuthService 类;
class AuthService {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Stream<String> get onAuthStateChanged => _firebaseAuth.onAuthStateChanged.map(
(FirebaseUser user) => user?.uid,
);
// Email & password Sign Up
Future<String> createUser(String email, String password, String companyName,
String nameAndSurname) async {
try {
final currentUser = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
// Update the UserName
var userUpdate = UserUpdateInfo();
userUpdate.displayName = companyName;
await currentUser.user.updateProfile(userUpdate);
await currentUser.user.reload();
Firestore.instance.runTransaction((Transaction transaction) async {
await Firestore.instance.collection("users").add({
currentUser.user.uid: {
"properties": {
"companyName": companyName,
"nameAndSurname": nameAndSurname,
"eMail": email,
"currentCashBalance": 0,
"currentTotalBalance": 0,
},
"partners": {},
"revenues": {},
"payments": {}
}
});
});
return currentUser.user.uid;
} catch (err) {
if (err is PlatformException) {
if (err.code == "ERROR_EMAIL_ALREADY_IN_USE") {
return "ERROR_EMAIL_ALREADY_IN_USE";
} else {
return err.code;
}
}
}
}
// Email & password Sign In
Future<String> signInWithEmailAndPassword(
String email, String password) async {
return (await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password))
.user
.uid;
}
// Sign Out
signOut() {
return _firebaseAuth.signOut();
}
// Get Current User
Future<FirebaseUser> getCurrentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user;
}
}
应用启动时的结果是这样的:
I/flutter (12529): snapshot data
I/flutter (12529): false
I/flutter (12529): snapshot data
I/flutter (12529): true
I/flutter (12529): *firebase userid*
我在这里做错了什么,我该如何做对?
你得到这个的原因是因为builder
在检索userid
之前被执行。
因此,您需要使用CircularProgressIndicator()
,它将显示循环进度,直到检索到future
的结果。 例如:
builder: (context, snapshot) {
if (snapshot.hasData) {
return Text(snapshot.data.uid);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
);
//function to check if the user is logged in
Future getCurrentUser() async {
FirebaseUser _user = await FirebaseAuth.instance.currentUser();
print("User: ${_user?.email ?? "None"}");
print(_user);
return _user;
}
//using future builder to show whichever screen you want to show loading screen is
//empty screen with circular progress indicator in center i hope this will help you
home: FutureBuilder(
future: getCurrentUser(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return LoadingScreen();
} else if (snapshot.connectionState == ConnectionState.done) {
return snapshot.data == null ? LoginScreen() : MainScreen();
}
return LoginScreen();
},
),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.