Я пытаюсь пропустить страницу входа и сразу перейти на домашнюю страницу в моем приложении, используя 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*
Что я здесь не так делаю и как мне это исправить?